Skip to content

Commit 8c4c820

Browse files
committed
Moved global code to main scope and added doctest for project euler problems 1 to 14.
1 parent 0e4e788 commit 8c4c820

File tree

37 files changed

+1257
-435
lines changed

37 files changed

+1257
-435
lines changed

project_euler/problem_01/sol1.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,21 @@
99
raw_input # Python 2
1010
except NameError:
1111
raw_input = input # Python 3
12-
n = int(raw_input().strip())
13-
print(sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0]))
12+
13+
def solution(n):
14+
"""Returns the sum of all the multiples of 3 or 5 below n.
15+
16+
>>> solution(3)
17+
0
18+
>>> solution(4)
19+
3
20+
>>> solution(10)
21+
23
22+
>>> solution(600)
23+
83700
24+
"""
25+
26+
return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0])
27+
28+
if __name__ == "__main__":
29+
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol2.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@
99
raw_input # Python 2
1010
except NameError:
1111
raw_input = input # Python 3
12-
n = int(raw_input().strip())
13-
sum = 0
14-
terms = (n-1)//3
15-
sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P.
16-
terms = (n-1)//5
17-
sum+= ((terms)*(10+(terms-1)*5))//2
18-
terms = (n-1)//15
19-
sum-= ((terms)*(30+(terms-1)*15))//2
20-
print(sum)
12+
13+
def solution(n):
14+
"""Returns the sum of all the multiples of 3 or 5 below n.
15+
16+
>>> solution(3)
17+
0
18+
>>> solution(4)
19+
3
20+
>>> solution(10)
21+
23
22+
>>> solution(600)
23+
83700
24+
"""
25+
26+
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
33+
return sum
34+
35+
if __name__ == "__main__":
36+
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol3.py

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,61 @@
1-
from __future__ import print_function
2-
31
'''
42
Problem Statement:
53
If we list all the natural numbers below 10 that are multiples of 3 or 5,
64
we get 3,5,6 and 9. The sum of these multiples is 23.
75
Find the sum of all the multiples of 3 or 5 below N.
86
'''
9-
'''
10-
This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.
11-
'''
12-
7+
from __future__ import print_function
138
try:
149
raw_input # Python 2
1510
except NameError:
1611
raw_input = input # Python 3
17-
n = int(raw_input().strip())
18-
sum=0
19-
num=0
20-
while(1):
21-
num+=3
22-
if(num>=n):
23-
break
24-
sum+=num
25-
num+=2
26-
if(num>=n):
27-
break
28-
sum+=num
29-
num+=1
30-
if(num>=n):
31-
break
32-
sum+=num
33-
num+=3
34-
if(num>=n):
35-
break
36-
sum+=num
37-
num+=1
38-
if(num>=n):
39-
break
40-
sum+=num
41-
num+=2
42-
if(num>=n):
43-
break
44-
sum+=num
45-
num+=3
46-
if(num>=n):
47-
break
48-
sum+=num
4912

50-
print(sum);
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+
def solution(n):
15+
"""Returns the sum of all the multiples of 3 or 5 below n.
16+
17+
>>> solution(3)
18+
0
19+
>>> solution(4)
20+
3
21+
>>> solution(10)
22+
23
23+
>>> solution(600)
24+
83700
25+
"""
26+
27+
sum=0
28+
num=0
29+
while(1):
30+
num+=3
31+
if(num>=n):
32+
break
33+
sum+=num
34+
num+=2
35+
if(num>=n):
36+
break
37+
sum+=num
38+
num+=1
39+
if(num>=n):
40+
break
41+
sum+=num
42+
num+=3
43+
if(num>=n):
44+
break
45+
sum+=num
46+
num+=1
47+
if(num>=n):
48+
break
49+
sum+=num
50+
num+=2
51+
if(num>=n):
52+
break
53+
sum+=num
54+
num+=3
55+
if(num>=n):
56+
break
57+
sum+=num
58+
return sum
59+
60+
if __name__ == "__main__":
61+
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol4.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
1-
def mulitples(limit):
1+
'''
2+
Problem Statement:
3+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
4+
we get 3,5,6 and 9. The sum of these multiples is 23.
5+
Find the sum of all the multiples of 3 or 5 below N.
6+
'''
7+
from __future__ import print_function
8+
try:
9+
raw_input # Python 2
10+
except NameError:
11+
raw_input = input # Python 3
12+
13+
def solution(n):
14+
"""Returns the sum of all the multiples of 3 or 5 below n.
15+
16+
>>> solution(3)
17+
0
18+
>>> solution(4)
19+
3
20+
>>> solution(10)
21+
23
22+
>>> solution(600)
23+
83700
24+
"""
25+
226
xmulti = []
327
zmulti = []
428
z = 3
529
x = 5
630
temp = 1
731
while True:
832
result = z * temp
9-
if (result < limit):
33+
if (result < n):
1034
zmulti.append(result)
1135
temp += 1
1236
else:
1337
temp = 1
1438
break
1539
while True:
1640
result = x * temp
17-
if (result < limit):
41+
if (result < n):
1842
xmulti.append(result)
1943
temp += 1
2044
else:
2145
break
2246
collection = list(set(xmulti+zmulti))
2347
return (sum(collection))
24-
25-
26-
27-
28-
29-
30-
print (mulitples(1000))
48+
49+
if __name__ == "__main__":
50+
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol5.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@
66
'''
77
from __future__ import print_function
88
try:
9-
input = raw_input #python3
9+
raw_input # Python 2
1010
except NameError:
11-
pass #python 2
11+
raw_input = input # Python 3
1212

1313
"""A straightforward pythonic solution using list comprehension"""
14-
n = int(input().strip())
15-
print(sum([i for i in range(n) if i%3==0 or i%5==0]))
14+
def solution(n):
15+
"""Returns the sum of all the multiples of 3 or 5 below n.
16+
17+
>>> solution(3)
18+
0
19+
>>> solution(4)
20+
3
21+
>>> solution(10)
22+
23
23+
>>> solution(600)
24+
83700
25+
"""
1626

27+
return sum([i for i in range(n) if i%3==0 or i%5==0])
28+
29+
if __name__ == "__main__":
30+
print(solution(int(raw_input().strip())))

project_euler/problem_01/sol6.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1-
a = 3
2-
result = 0
3-
while a < 1000:
4-
if(a % 3 == 0 or a % 5 == 0):
5-
result += a
6-
elif(a % 15 == 0):
7-
result -= a
8-
a += 1
9-
print(result)
1+
'''
2+
Problem Statement:
3+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
4+
we get 3,5,6 and 9. The sum of these multiples is 23.
5+
Find the sum of all the multiples of 3 or 5 below N.
6+
'''
7+
from __future__ import print_function
8+
try:
9+
raw_input # Python 2
10+
except NameError:
11+
raw_input = input # Python 3
12+
13+
def solution(n):
14+
"""Returns the sum of all the multiples of 3 or 5 below n.
15+
16+
>>> solution(3)
17+
0
18+
>>> solution(4)
19+
3
20+
>>> solution(10)
21+
23
22+
>>> solution(600)
23+
83700
24+
"""
25+
26+
a = 3
27+
result = 0
28+
while a < n:
29+
if(a % 3 == 0 or a % 5 == 0):
30+
result += a
31+
elif(a % 15 == 0):
32+
result -= a
33+
a += 1
34+
return result
35+
36+
if __name__ == "__main__":
37+
print(solution(int(raw_input().strip())))

project_euler/problem_02/sol1.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@
1313
except NameError:
1414
raw_input = input # Python 3
1515

16-
n = int(raw_input().strip())
17-
i=1
18-
j=2
19-
sum=0
20-
while(j<=n):
21-
if j%2 == 0:
22-
sum+=j
23-
i , j = j, i+j
24-
print(sum)
16+
def solution(n):
17+
"""Returns the sum of all fibonacci sequence even elements that are lower or equals to n.
18+
19+
>>> solution(10)
20+
10
21+
>>> solution(15)
22+
10
23+
>>> solution(2)
24+
2
25+
>>> solution(1)
26+
0
27+
>>> solution(34)
28+
44
29+
"""
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
37+
38+
return sum
39+
40+
if __name__ == "__main__":
41+
print(solution(int(raw_input().strip())))

project_euler/problem_02/sol2.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
def fib(n):
2-
"""
3-
Returns a list of all the even terms in the Fibonacci sequence that are less than n.
1+
'''
2+
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+
'''
9+
from __future__ import print_function
10+
11+
try:
12+
raw_input # Python 2
13+
except NameError:
14+
raw_input = input # Python 3
15+
16+
def solution(n):
17+
"""Returns the sum of all fibonacci sequence even elements that are lower or equals to n.
18+
19+
>>> solution(10)
20+
[2, 8]
21+
>>> solution(15)
22+
[2, 8]
23+
>>> solution(2)
24+
[2]
25+
>>> solution(1)
26+
[]
27+
>>> solution(34)
28+
[2, 8, 34]
429
"""
530
ls = []
631
a, b = 0, 1
7-
while b < n:
32+
while b <= n:
833
if b % 2 == 0:
934
ls.append(b)
1035
a, b = b, a+b
1136
return ls
1237

13-
if __name__ == '__main__':
14-
n = int(input("Enter max number: ").strip())
15-
print(sum(fib(n)))
38+
if __name__ == "__main__":
39+
print(solution(int(raw_input().strip())))

0 commit comments

Comments
 (0)