Skip to content

Commit 6c11811

Browse files
committed
Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts.
1 parent 8b17220 commit 6c11811

File tree

59 files changed

+1558
-1278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1558
-1278
lines changed

project_euler/problem_01/sol1.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
'''
1+
"""
22
Problem Statement:
33
If we list all the natural numbers below 10 that are multiples of 3 or 5,
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
6-
'''
6+
"""
77
from __future__ import print_function
8+
89
try:
9-
raw_input # Python 2
10+
raw_input # Python 2
1011
except NameError:
1112
raw_input = input # Python 3
1213

14+
1315
def solution(n):
1416
"""Returns the sum of all the multiples of 3 or 5 below n.
1517
@@ -27,5 +29,6 @@ def solution(n):
2729

2830
return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0])
2931

32+
3033
if __name__ == "__main__":
3134
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol2.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
'''
1+
"""
22
Problem Statement:
33
If we list all the natural numbers below 10 that are multiples of 3 or 5,
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
6-
'''
6+
"""
77
from __future__ import print_function
8+
89
try:
9-
raw_input # Python 2
10+
raw_input # Python 2
1011
except NameError:
1112
raw_input = input # Python 3
1213

14+
1315
def solution(n):
1416
"""Returns the sum of all the multiples of 3 or 5 below n.
1517
@@ -24,13 +26,14 @@ def solution(n):
2426
"""
2527

2628
sum = 0
27-
terms = (n-1)//3
28-
sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P.
29-
terms = (n-1)//5
30-
sum+= ((terms)*(10+(terms-1)*5))//2
31-
terms = (n-1)//15
32-
sum-= ((terms)*(30+(terms-1)*15))//2
29+
terms = (n - 1) // 3
30+
sum += ((terms) * (6 + (terms - 1) * 3)) // 2 # sum of an A.P.
31+
terms = (n - 1) // 5
32+
sum += ((terms) * (10 + (terms - 1) * 5)) // 2
33+
terms = (n - 1) // 15
34+
sum -= ((terms) * (30 + (terms - 1) * 15)) // 2
3335
return sum
3436

37+
3538
if __name__ == "__main__":
3639
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol3.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
'''
1+
"""
22
Problem Statement:
33
If we list all the natural numbers below 10 that are multiples of 3 or 5,
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
6-
'''
6+
"""
77
from __future__ import print_function
8+
89
try:
9-
raw_input # Python 2
10+
raw_input # Python 2
1011
except NameError:
1112
raw_input = input # Python 3
1213

13-
"""This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3."""
14+
1415
def solution(n):
15-
"""Returns the sum of all the multiples of 3 or 5 below n.
16+
"""
17+
This solution is based on the pattern that the successive numbers in the
18+
series follow: 0+3,+2,+1,+3,+1,+2,+3.
19+
Returns the sum of all the multiples of 3 or 5 below n.
1620
1721
>>> solution(3)
1822
0
@@ -24,38 +28,39 @@ def solution(n):
2428
83700
2529
"""
2630

27-
sum=0
28-
num=0
29-
while(1):
30-
num+=3
31-
if(num>=n):
31+
sum = 0
32+
num = 0
33+
while 1:
34+
num += 3
35+
if num >= n:
3236
break
33-
sum+=num
34-
num+=2
35-
if(num>=n):
37+
sum += num
38+
num += 2
39+
if num >= n:
3640
break
37-
sum+=num
38-
num+=1
39-
if(num>=n):
41+
sum += num
42+
num += 1
43+
if num >= n:
4044
break
41-
sum+=num
42-
num+=3
43-
if(num>=n):
45+
sum += num
46+
num += 3
47+
if num >= n:
4448
break
45-
sum+=num
46-
num+=1
47-
if(num>=n):
49+
sum += num
50+
num += 1
51+
if num >= n:
4852
break
49-
sum+=num
50-
num+=2
51-
if(num>=n):
53+
sum += num
54+
num += 2
55+
if num >= n:
5256
break
53-
sum+=num
54-
num+=3
55-
if(num>=n):
57+
sum += num
58+
num += 3
59+
if num >= n:
5660
break
57-
sum+=num
61+
sum += num
5862
return sum
5963

64+
6065
if __name__ == "__main__":
6166
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol4.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
'''
1+
"""
22
Problem Statement:
33
If we list all the natural numbers below 10 that are multiples of 3 or 5,
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
6-
'''
6+
"""
77
from __future__ import print_function
8+
89
try:
9-
raw_input # Python 2
10+
raw_input # Python 2
1011
except NameError:
1112
raw_input = input # Python 3
1213

14+
1315
def solution(n):
1416
"""Returns the sum of all the multiples of 3 or 5 below n.
1517
@@ -30,21 +32,22 @@ def solution(n):
3032
temp = 1
3133
while True:
3234
result = z * temp
33-
if (result < n):
35+
if result < n:
3436
zmulti.append(result)
3537
temp += 1
3638
else:
3739
temp = 1
3840
break
3941
while True:
4042
result = x * temp
41-
if (result < n):
43+
if result < n:
4244
xmulti.append(result)
4345
temp += 1
4446
else:
4547
break
46-
collection = list(set(xmulti+zmulti))
47-
return (sum(collection))
48+
collection = list(set(xmulti + zmulti))
49+
return sum(collection)
50+
4851

4952
if __name__ == "__main__":
5053
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol5.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
'''
1+
"""
22
Problem Statement:
33
If we list all the natural numbers below 10 that are multiples of 3 or 5,
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
6-
'''
6+
"""
77
from __future__ import print_function
8+
89
try:
9-
raw_input # Python 2
10+
raw_input # Python 2
1011
except NameError:
1112
raw_input = input # Python 3
1213

1314
"""A straightforward pythonic solution using list comprehension"""
15+
16+
1417
def solution(n):
1518
"""Returns the sum of all the multiples of 3 or 5 below n.
1619
@@ -24,7 +27,8 @@ def solution(n):
2427
83700
2528
"""
2629

27-
return sum([i for i in range(n) if i%3==0 or i%5==0])
30+
return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0])
31+
2832

2933
if __name__ == "__main__":
3034
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol6.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
'''
1+
"""
22
Problem Statement:
33
If we list all the natural numbers below 10 that are multiples of 3 or 5,
44
we get 3,5,6 and 9. The sum of these multiples is 23.
55
Find the sum of all the multiples of 3 or 5 below N.
6-
'''
6+
"""
77
from __future__ import print_function
8+
89
try:
9-
raw_input # Python 2
10+
raw_input # Python 2
1011
except NameError:
1112
raw_input = input # Python 3
1213

14+
1315
def solution(n):
1416
"""Returns the sum of all the multiples of 3 or 5 below n.
1517
@@ -26,12 +28,13 @@ def solution(n):
2628
a = 3
2729
result = 0
2830
while a < n:
29-
if(a % 3 == 0 or a % 5 == 0):
31+
if a % 3 == 0 or a % 5 == 0:
3032
result += a
31-
elif(a % 15 == 0):
33+
elif a % 15 == 0:
3234
result -= a
3335
a += 1
3436
return result
3537

38+
3639
if __name__ == "__main__":
3740
print(solution(int(raw_input().strip())))

project_euler/problem_02/sol1.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
'''
1+
"""
22
Problem:
3-
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2,
4-
the first 10 terms will be:
5-
1,2,3,5,8,13,21,34,55,89,..
6-
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
7-
e.g. for n=10, we have {2,8}, sum is 10.
8-
'''
3+
Each new term in the Fibonacci sequence is generated by adding the previous two
4+
terms. By starting with 1 and 2, the first 10 terms will be:
5+
6+
1,2,3,5,8,13,21,34,55,89,..
7+
8+
By considering the terms in the Fibonacci sequence whose values do not exceed
9+
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
10+
10.
11+
"""
912
from __future__ import print_function
1013

1114
try:
12-
raw_input # Python 2
15+
raw_input # Python 2
1316
except NameError:
1417
raw_input = input # Python 3
1518

19+
1620
def solution(n):
17-
"""Returns the sum of all fibonacci sequence even elements that are lower or equals to n.
21+
"""Returns the sum of all fibonacci sequence even elements that are lower
22+
or equals to n.
1823
1924
>>> solution(10)
2025
10
@@ -27,15 +32,16 @@ def solution(n):
2732
>>> solution(34)
2833
44
2934
"""
30-
i=1
31-
j=2
32-
sum=0
33-
while(j<=n):
34-
if j%2 == 0:
35-
sum+=j
36-
i , j = j, i+j
35+
i = 1
36+
j = 2
37+
sum = 0
38+
while j <= n:
39+
if j % 2 == 0:
40+
sum += j
41+
i, j = j, i + j
3742

3843
return sum
3944

45+
4046
if __name__ == "__main__":
4147
print(solution(int(raw_input().strip())))

project_euler/problem_02/sol2.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
'''
1+
"""
22
Problem:
3-
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2,
4-
the first 10 terms will be:
5-
1,2,3,5,8,13,21,34,55,89,..
6-
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
7-
e.g. for n=10, we have {2,8}, sum is 10.
8-
'''
3+
Each new term in the Fibonacci sequence is generated by adding the previous two
4+
terms. By starting with 1 and 2, the first 10 terms will be:
5+
6+
1,2,3,5,8,13,21,34,55,89,..
7+
8+
By considering the terms in the Fibonacci sequence whose values do not exceed
9+
n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is
10+
10.
11+
"""
912
from __future__ import print_function
1013

1114
try:
12-
raw_input # Python 2
15+
raw_input # Python 2
1316
except NameError:
1417
raw_input = input # Python 3
1518

19+
1620
def solution(n):
17-
"""Returns the sum of all fibonacci sequence even elements that are lower or equals to n.
21+
"""Returns the sum of all fibonacci sequence even elements that are lower
22+
or equals to n.
1823
1924
>>> solution(10)
2025
[2, 8]
@@ -32,8 +37,9 @@ def solution(n):
3237
while b <= n:
3338
if b % 2 == 0:
3439
ls.append(b)
35-
a, b = b, a+b
40+
a, b = b, a + b
3641
return ls
3742

43+
3844
if __name__ == "__main__":
3945
print(solution(int(raw_input().strip())))

0 commit comments

Comments
 (0)