Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 98e9d6b

Browse files
authoredOct 25, 2020
Fix style of the first ten solutions for Project Euler (TheAlgorithms#3242)
* Fix style of the first ten solutions for Project Euler - Unify the header docstring, and add reference URLs to wikipedia or similar - Fix docstrings to be properly multilined - Add newlines where appropriate - Add doctests where they were missing - Remove doctests that test for the correct solution - fix obvious spelling or grammar mistakes in comments and exception messages - Fix line endings to be UNIX. This makes two of the files seem to have changed completely - no functional changes in any of the solutions were done (except for the spelling fixes mentioned above) * Fix docstrings and main function as per Style Guide
1 parent 5be77f3 commit 98e9d6b

File tree

35 files changed

+716
-468
lines changed

35 files changed

+716
-468
lines changed
 

‎project_euler/problem_001/sol1.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""
2-
Problem Statement:
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
36
If we list all the natural numbers below 10 that are multiples of 3 or 5,
47
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.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
610
"""
711

812

913
def solution(n: int = 1000) -> int:
10-
"""Returns the sum of all the multiples of 3 or 5 below n.
14+
"""
15+
Returns the sum of all the multiples of 3 or 5 below n.
1116
1217
>>> solution(3)
1318
0
@@ -25,4 +30,4 @@ def solution(n: int = 1000) -> int:
2530

2631

2732
if __name__ == "__main__":
28-
print(solution(int(input().strip())))
33+
print(f"{solution() = }")

‎project_euler/problem_001/sol2.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""
2-
Problem Statement:
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
36
If we list all the natural numbers below 10 that are multiples of 3 or 5,
47
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.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
610
"""
711

812

913
def solution(n: int = 1000) -> int:
10-
"""Returns the sum of all the multiples of 3 or 5 below n.
14+
"""
15+
Returns the sum of all the multiples of 3 or 5 below n.
1116
1217
>>> solution(3)
1318
0
@@ -30,4 +35,4 @@ def solution(n: int = 1000) -> int:
3035

3136

3237
if __name__ == "__main__":
33-
print(solution(int(input().strip())))
38+
print(f"{solution() = }")

‎project_euler/problem_001/sol3.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
"""
2-
Problem Statement:
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
36
If we list all the natural numbers below 10 that are multiples of 3 or 5,
47
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.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
610
"""
711

812

@@ -57,4 +61,4 @@ def solution(n: int = 1000) -> int:
5761

5862

5963
if __name__ == "__main__":
60-
print(solution(int(input().strip())))
64+
print(f"{solution() = }")

‎project_euler/problem_001/sol4.py

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
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-
8-
9-
def solution(n: int = 1000) -> int:
10-
"""Returns the sum of all the multiples of 3 or 5 below n.
11-
12-
>>> solution(3)
13-
0
14-
>>> solution(4)
15-
3
16-
>>> solution(10)
17-
23
18-
>>> solution(600)
19-
83700
20-
"""
21-
22-
xmulti = []
23-
zmulti = []
24-
z = 3
25-
x = 5
26-
temp = 1
27-
while True:
28-
result = z * temp
29-
if result < n:
30-
zmulti.append(result)
31-
temp += 1
32-
else:
33-
temp = 1
34-
break
35-
while True:
36-
result = x * temp
37-
if result < n:
38-
xmulti.append(result)
39-
temp += 1
40-
else:
41-
break
42-
collection = list(set(xmulti + zmulti))
43-
return sum(collection)
44-
45-
46-
if __name__ == "__main__":
47-
print(solution(int(input().strip())))
1+
"""
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
6+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
7+
we get 3, 5, 6 and 9. The sum of these multiples is 23.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
10+
"""
11+
12+
13+
def solution(n: int = 1000) -> int:
14+
"""
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+
xmulti = []
28+
zmulti = []
29+
z = 3
30+
x = 5
31+
temp = 1
32+
while True:
33+
result = z * temp
34+
if result < n:
35+
zmulti.append(result)
36+
temp += 1
37+
else:
38+
temp = 1
39+
break
40+
while True:
41+
result = x * temp
42+
if result < n:
43+
xmulti.append(result)
44+
temp += 1
45+
else:
46+
break
47+
collection = list(set(xmulti + zmulti))
48+
return sum(collection)
49+
50+
51+
if __name__ == "__main__":
52+
print(f"{solution() = }")

‎project_euler/problem_001/sol5.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
"""
2-
Problem Statement:
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
36
If we list all the natural numbers below 10 that are multiples of 3 or 5,
47
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.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
610
"""
711

812

913
def solution(n: int = 1000) -> int:
10-
"""Returns the sum of all the multiples of 3 or 5 below n.
11-
A straightforward pythonic solution using list comprehension.
14+
"""
15+
Returns the sum of all the multiples of 3 or 5 below n.
16+
A straightforward pythonic solution using list comprehension.
1217
1318
>>> solution(3)
1419
0
@@ -24,4 +29,4 @@ def solution(n: int = 1000) -> int:
2429

2530

2631
if __name__ == "__main__":
27-
print(solution(int(input().strip())))
32+
print(f"{solution() = }")

‎project_euler/problem_001/sol6.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""
2-
Problem Statement:
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
36
If we list all the natural numbers below 10 that are multiples of 3 or 5,
47
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.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
610
"""
711

812

913
def solution(n: int = 1000) -> int:
10-
"""Returns the sum of all the multiples of 3 or 5 below n.
14+
"""
15+
Returns the sum of all the multiples of 3 or 5 below n.
1116
1217
>>> solution(3)
1318
0
@@ -31,4 +36,4 @@ def solution(n: int = 1000) -> int:
3136

3237

3338
if __name__ == "__main__":
34-
print(solution(int(input().strip())))
39+
print(f"{solution() = }")

‎project_euler/problem_001/sol7.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""
2-
Problem Statement:
2+
Project Euler Problem 1: https://projecteuler.net/problem=1
3+
4+
Multiples of 3 and 5
5+
36
If we list all the natural numbers below 10 that are multiples of 3 or 5,
47
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.
8+
9+
Find the sum of all the multiples of 3 or 5 below 1000.
610
"""
711

812

913
def solution(n: int = 1000) -> int:
10-
"""Returns the sum of all the multiples of 3 or 5 below n.
14+
"""
15+
Returns the sum of all the multiples of 3 or 5 below n.
1116
1217
>>> solution(3)
1318
0
@@ -29,4 +34,4 @@ def solution(n: int = 1000) -> int:
2934

3035

3136
if __name__ == "__main__":
32-
print(solution(int(input().strip())))
37+
print(f"{solution() = }")

‎project_euler/problem_002/sol1.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
"""
2-
Problem:
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:
2+
Project Euler Problem 2: https://projecteuler.net/problem=2
53
6-
1,2,3,5,8,13,21,34,55,89,..
4+
Even Fibonacci Numbers
5+
6+
Each new term in the Fibonacci sequence is generated by adding the previous
7+
two terms. By starting with 1 and 2, the first 10 terms will be:
8+
9+
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
710
811
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.
12+
four million, find the sum of the even-valued terms.
13+
14+
References:
15+
- https://en.wikipedia.org/wiki/Fibonacci_number
1116
"""
1217

1318

1419
def solution(n: int = 4000000) -> int:
15-
"""Returns the sum of all fibonacci sequence even elements that are lower
16-
or equals to n.
20+
"""
21+
Returns the sum of all even fibonacci sequence elements that are lower
22+
or equal to n.
1723
1824
>>> solution(10)
1925
10
@@ -26,6 +32,7 @@ def solution(n: int = 4000000) -> int:
2632
>>> solution(34)
2733
44
2834
"""
35+
2936
i = 1
3037
j = 2
3138
total = 0
@@ -38,4 +45,4 @@ def solution(n: int = 4000000) -> int:
3845

3946

4047
if __name__ == "__main__":
41-
print(solution(int(input().strip())))
48+
print(f"{solution() = }")

‎project_euler/problem_002/sol2.py

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1-
"""
2-
Problem:
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-
"""
12-
13-
14-
def solution(n: int = 4000000) -> int:
15-
"""Returns the sum of all fibonacci sequence even elements that are lower
16-
or equals to n.
17-
18-
>>> solution(10)
19-
10
20-
>>> solution(15)
21-
10
22-
>>> solution(2)
23-
2
24-
>>> solution(1)
25-
0
26-
>>> solution(34)
27-
44
28-
"""
29-
even_fibs = []
30-
a, b = 0, 1
31-
while b <= n:
32-
if b % 2 == 0:
33-
even_fibs.append(b)
34-
a, b = b, a + b
35-
return sum(even_fibs)
36-
37-
38-
if __name__ == "__main__":
39-
print(solution(int(input().strip())))
1+
"""
2+
Project Euler Problem 2: https://projecteuler.net/problem=2
3+
4+
Even Fibonacci Numbers
5+
6+
Each new term in the Fibonacci sequence is generated by adding the previous
7+
two terms. By starting with 1 and 2, the first 10 terms will be:
8+
9+
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
10+
11+
By considering the terms in the Fibonacci sequence whose values do not exceed
12+
four million, find the sum of the even-valued terms.
13+
14+
References:
15+
- https://en.wikipedia.org/wiki/Fibonacci_number
16+
"""
17+
18+
19+
def solution(n: int = 4000000) -> int:
20+
"""
21+
Returns the sum of all even fibonacci sequence elements that are lower
22+
or equal to n.
23+
24+
>>> solution(10)
25+
10
26+
>>> solution(15)
27+
10
28+
>>> solution(2)
29+
2
30+
>>> solution(1)
31+
0
32+
>>> solution(34)
33+
44
34+
"""
35+
36+
even_fibs = []
37+
a, b = 0, 1
38+
while b <= n:
39+
if b % 2 == 0:
40+
even_fibs.append(b)
41+
a, b = b, a + b
42+
return sum(even_fibs)
43+
44+
45+
if __name__ == "__main__":
46+
print(f"{solution() = }")

‎project_euler/problem_002/sol3.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
"""
2-
Problem:
2+
Project Euler Problem 2: https://projecteuler.net/problem=2
3+
4+
Even Fibonacci Numbers
5+
36
Each new term in the Fibonacci sequence is generated by adding the previous
47
two terms. By starting with 1 and 2, the first 10 terms will be:
58
6-
1,2,3,5,8,13,21,34,55,89,..
9+
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
710
811
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.
12+
four million, find the sum of the even-valued terms.
13+
14+
References:
15+
- https://en.wikipedia.org/wiki/Fibonacci_number
1116
"""
1217

1318

1419
def solution(n: int = 4000000) -> int:
15-
"""Returns the sum of all fibonacci sequence even elements that are lower
16-
or equals to n.
20+
"""
21+
Returns the sum of all even fibonacci sequence elements that are lower
22+
or equal to n.
1723
1824
>>> solution(10)
1925
10
@@ -26,6 +32,7 @@ def solution(n: int = 4000000) -> int:
2632
>>> solution(34)
2733
44
2834
"""
35+
2936
if n <= 1:
3037
return 0
3138
a = 0
@@ -38,4 +45,4 @@ def solution(n: int = 4000000) -> int:
3845

3946

4047
if __name__ == "__main__":
41-
print(solution(int(input().strip())))
48+
print(f"{solution() = }")

‎project_euler/problem_002/sol4.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
"""
2-
Problem:
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:
2+
Project Euler Problem 2: https://projecteuler.net/problem=2
53
6-
1,2,3,5,8,13,21,34,55,89,..
4+
Even Fibonacci Numbers
5+
6+
Each new term in the Fibonacci sequence is generated by adding the previous
7+
two terms. By starting with 1 and 2, the first 10 terms will be:
8+
9+
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
710
811
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.
12+
four million, find the sum of the even-valued terms.
13+
14+
References:
15+
- https://en.wikipedia.org/wiki/Fibonacci_number
1116
"""
1217
import math
1318
from decimal import Decimal, getcontext
1419

1520

1621
def solution(n: int = 4000000) -> int:
17-
"""Returns the sum of all fibonacci sequence even elements that are lower
18-
or equals to n.
22+
"""
23+
Returns the sum of all even fibonacci sequence elements that are lower
24+
or equal to n.
1925
2026
>>> solution(10)
2127
10
@@ -32,26 +38,27 @@ def solution(n: int = 4000000) -> int:
3238
>>> solution(0)
3339
Traceback (most recent call last):
3440
...
35-
ValueError: Parameter n must be greater or equal to one.
41+
ValueError: Parameter n must be greater than or equal to one.
3642
>>> solution(-17)
3743
Traceback (most recent call last):
3844
...
39-
ValueError: Parameter n must be greater or equal to one.
45+
ValueError: Parameter n must be greater than or equal to one.
4046
>>> solution([])
4147
Traceback (most recent call last):
4248
...
43-
TypeError: Parameter n must be int or passive of cast to int.
49+
TypeError: Parameter n must be int or castable to int.
4450
>>> solution("asd")
4551
Traceback (most recent call last):
4652
...
47-
TypeError: Parameter n must be int or passive of cast to int.
53+
TypeError: Parameter n must be int or castable to int.
4854
"""
55+
4956
try:
5057
n = int(n)
5158
except (TypeError, ValueError):
52-
raise TypeError("Parameter n must be int or passive of cast to int.")
59+
raise TypeError("Parameter n must be int or castable to int.")
5360
if n <= 0:
54-
raise ValueError("Parameter n must be greater or equal to one.")
61+
raise ValueError("Parameter n must be greater than or equal to one.")
5562
getcontext().prec = 100
5663
phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2)
5764

@@ -62,4 +69,4 @@ def solution(n: int = 4000000) -> int:
6269

6370

6471
if __name__ == "__main__":
65-
print(solution(int(input().strip())))
72+
print(f"{solution() = }")

‎project_euler/problem_002/sol5.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
"""
2-
Problem:
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:
2+
Project Euler Problem 2: https://projecteuler.net/problem=2
53
6-
1,2,3,5,8,13,21,34,55,89,..
4+
Even Fibonacci Numbers
5+
6+
Each new term in the Fibonacci sequence is generated by adding the previous
7+
two terms. By starting with 1 and 2, the first 10 terms will be:
8+
9+
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
710
811
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.
12+
four million, find the sum of the even-valued terms.
13+
14+
References:
15+
- https://en.wikipedia.org/wiki/Fibonacci_number
1116
"""
1217

1318

1419
def solution(n: int = 4000000) -> int:
15-
"""Returns the sum of all fibonacci sequence even elements that are lower
16-
or equals to n.
20+
"""
21+
Returns the sum of all even fibonacci sequence elements that are lower
22+
or equal to n.
1723
1824
>>> solution(10)
1925
10
@@ -43,4 +49,4 @@ def solution(n: int = 4000000) -> int:
4349

4450

4551
if __name__ == "__main__":
46-
print(solution(int(input().strip())))
52+
print(f"{solution() = }")

‎project_euler/problem_003/sol1.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
"""
2-
Problem:
3-
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor
4-
of a given number N?
2+
Project Euler Problem 3: https://projecteuler.net/problem=3
53
6-
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
7-
"""
4+
Largest prime factor
5+
6+
The prime factors of 13195 are 5, 7, 13 and 29.
7+
8+
What is the largest prime factor of the number 600851475143?
89
10+
References:
11+
- https://en.wikipedia.org/wiki/Prime_number#Unique_factorization
12+
"""
913
import math
1014

1115

1216
def isprime(num: int) -> bool:
13-
"""Returns boolean representing primality of given number num.
17+
"""
18+
Returns boolean representing primality of given number num.
19+
1420
>>> isprime(2)
1521
True
1622
>>> isprime(3)
@@ -22,14 +28,15 @@ def isprime(num: int) -> bool:
2228
>>> isprime(0)
2329
Traceback (most recent call last):
2430
...
25-
ValueError: Parameter num must be greater or equal to two.
31+
ValueError: Parameter num must be greater than or equal to two.
2632
>>> isprime(1)
2733
Traceback (most recent call last):
2834
...
29-
ValueError: Parameter num must be greater or equal to two.
35+
ValueError: Parameter num must be greater than or equal to two.
3036
"""
37+
3138
if num <= 1:
32-
raise ValueError("Parameter num must be greater or equal to two.")
39+
raise ValueError("Parameter num must be greater than or equal to two.")
3340
if num == 2:
3441
return True
3542
elif num % 2 == 0:
@@ -41,7 +48,9 @@ def isprime(num: int) -> bool:
4148

4249

4350
def solution(n: int = 600851475143) -> int:
44-
"""Returns the largest prime factor of a given number n.
51+
"""
52+
Returns the largest prime factor of a given number n.
53+
4554
>>> solution(13195)
4655
29
4756
>>> solution(10)
@@ -53,26 +62,27 @@ def solution(n: int = 600851475143) -> int:
5362
>>> solution(0)
5463
Traceback (most recent call last):
5564
...
56-
ValueError: Parameter n must be greater or equal to one.
65+
ValueError: Parameter n must be greater than or equal to one.
5766
>>> solution(-17)
5867
Traceback (most recent call last):
5968
...
60-
ValueError: Parameter n must be greater or equal to one.
69+
ValueError: Parameter n must be greater than or equal to one.
6170
>>> solution([])
6271
Traceback (most recent call last):
6372
...
64-
TypeError: Parameter n must be int or passive of cast to int.
73+
TypeError: Parameter n must be int or castable to int.
6574
>>> solution("asd")
6675
Traceback (most recent call last):
6776
...
68-
TypeError: Parameter n must be int or passive of cast to int.
77+
TypeError: Parameter n must be int or castable to int.
6978
"""
79+
7080
try:
7181
n = int(n)
7282
except (TypeError, ValueError):
73-
raise TypeError("Parameter n must be int or passive of cast to int.")
83+
raise TypeError("Parameter n must be int or castable to int.")
7484
if n <= 0:
75-
raise ValueError("Parameter n must be greater or equal to one.")
85+
raise ValueError("Parameter n must be greater than or equal to one.")
7686
max_number = 0
7787
if isprime(n):
7888
return n
@@ -91,4 +101,4 @@ def solution(n: int = 600851475143) -> int:
91101

92102

93103
if __name__ == "__main__":
94-
print(solution(int(input().strip())))
104+
print(f"{solution() = }")

‎project_euler/problem_003/sol2.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
"""
2-
Problem:
3-
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor
4-
of a given number N?
2+
Project Euler Problem 3: https://projecteuler.net/problem=3
53
6-
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
4+
Largest prime factor
5+
6+
The prime factors of 13195 are 5, 7, 13 and 29.
7+
8+
What is the largest prime factor of the number 600851475143?
9+
10+
References:
11+
- https://en.wikipedia.org/wiki/Prime_number#Unique_factorization
712
"""
813

914

1015
def solution(n: int = 600851475143) -> int:
11-
"""Returns the largest prime factor of a given number n.
16+
"""
17+
Returns the largest prime factor of a given number n.
18+
1219
>>> solution(13195)
1320
29
1421
>>> solution(10)
@@ -20,26 +27,27 @@ def solution(n: int = 600851475143) -> int:
2027
>>> solution(0)
2128
Traceback (most recent call last):
2229
...
23-
ValueError: Parameter n must be greater or equal to one.
30+
ValueError: Parameter n must be greater than or equal to one.
2431
>>> solution(-17)
2532
Traceback (most recent call last):
2633
...
27-
ValueError: Parameter n must be greater or equal to one.
34+
ValueError: Parameter n must be greater than or equal to one.
2835
>>> solution([])
2936
Traceback (most recent call last):
3037
...
31-
TypeError: Parameter n must be int or passive of cast to int.
38+
TypeError: Parameter n must be int or castable to int.
3239
>>> solution("asd")
3340
Traceback (most recent call last):
3441
...
35-
TypeError: Parameter n must be int or passive of cast to int.
42+
TypeError: Parameter n must be int or castable to int.
3643
"""
44+
3745
try:
3846
n = int(n)
3947
except (TypeError, ValueError):
40-
raise TypeError("Parameter n must be int or passive of cast to int.")
48+
raise TypeError("Parameter n must be int or castable to int.")
4149
if n <= 0:
42-
raise ValueError("Parameter n must be greater or equal to one.")
50+
raise ValueError("Parameter n must be greater than or equal to one.")
4351
prime = 1
4452
i = 2
4553
while i * i <= n:
@@ -53,4 +61,4 @@ def solution(n: int = 600851475143) -> int:
5361

5462

5563
if __name__ == "__main__":
56-
print(solution(int(input().strip())))
64+
print(f"{solution() = }")

‎project_euler/problem_003/sol3.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
"""
2-
Problem:
3-
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor
4-
of a given number N?
2+
Project Euler Problem 3: https://projecteuler.net/problem=3
53
6-
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
4+
Largest prime factor
5+
6+
The prime factors of 13195 are 5, 7, 13 and 29.
7+
8+
What is the largest prime factor of the number 600851475143?
9+
10+
References:
11+
- https://en.wikipedia.org/wiki/Prime_number#Unique_factorization
712
"""
813

914

1015
def solution(n: int = 600851475143) -> int:
11-
"""Returns the largest prime factor of a given number n.
16+
"""
17+
Returns the largest prime factor of a given number n.
18+
1219
>>> solution(13195)
1320
29
1421
>>> solution(10)
@@ -20,26 +27,27 @@ def solution(n: int = 600851475143) -> int:
2027
>>> solution(0)
2128
Traceback (most recent call last):
2229
...
23-
ValueError: Parameter n must be greater or equal to one.
30+
ValueError: Parameter n must be greater than or equal to one.
2431
>>> solution(-17)
2532
Traceback (most recent call last):
2633
...
27-
ValueError: Parameter n must be greater or equal to one.
34+
ValueError: Parameter n must be greater than or equal to one.
2835
>>> solution([])
2936
Traceback (most recent call last):
3037
...
31-
TypeError: Parameter n must be int or passive of cast to int.
38+
TypeError: Parameter n must be int or castable to int.
3239
>>> solution("asd")
3340
Traceback (most recent call last):
3441
...
35-
TypeError: Parameter n must be int or passive of cast to int.
42+
TypeError: Parameter n must be int or castable to int.
3643
"""
44+
3745
try:
3846
n = int(n)
3947
except (TypeError, ValueError):
40-
raise TypeError("Parameter n must be int or passive of cast to int.")
48+
raise TypeError("Parameter n must be int or castable to int.")
4149
if n <= 0:
42-
raise ValueError("Parameter n must be greater or equal to one.")
50+
raise ValueError("Parameter n must be greater than or equal to one.")
4351
i = 2
4452
ans = 0
4553
if n == 2:
@@ -55,4 +63,4 @@ def solution(n: int = 600851475143) -> int:
5563

5664

5765
if __name__ == "__main__":
58-
print(solution(int(input().strip())))
66+
print(f"{solution() = }")

‎project_euler/problem_004/sol1.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
"""
2-
Problem:
3-
A palindromic number reads the same both ways. The largest palindrome made from
4-
the product of two 2-digit numbers is 9009 = 91 x 99.
2+
Project Euler Problem 4: https://projecteuler.net/problem=4
53
6-
Find the largest palindrome made from the product of two 3-digit numbers which
7-
is less than N.
4+
Largest palindrome product
5+
6+
A palindromic number reads the same both ways. The largest palindrome made
7+
from the product of two 2-digit numbers is 9009 = 91 × 99.
8+
9+
Find the largest palindrome made from the product of two 3-digit numbers.
10+
11+
References:
12+
- https://en.wikipedia.org/wiki/Palindromic_number
813
"""
914

1015

1116
def solution(n: int = 998001) -> int:
12-
"""Returns the largest palindrome made from the product of two 3-digit
17+
"""
18+
Returns the largest palindrome made from the product of two 3-digit
1319
numbers which is less than n.
1420
1521
>>> solution(20000)
@@ -23,10 +29,10 @@ def solution(n: int = 998001) -> int:
2329
...
2430
ValueError: That number is larger than our acceptable range.
2531
"""
32+
2633
# fetches the next number
2734
for number in range(n - 1, 9999, -1):
2835

29-
# converts number into string.
3036
str_number = str(number)
3137

3238
# checks whether 'str_number' is a palindrome.
@@ -44,8 +50,4 @@ def solution(n: int = 998001) -> int:
4450

4551

4652
if __name__ == "__main__":
47-
import doctest
48-
49-
doctest.testmod()
50-
51-
print(solution(int(input().strip())))
53+
print(f"{solution() = }")

‎project_euler/problem_004/sol2.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
"""
2-
Problem:
3-
A palindromic number reads the same both ways. The largest palindrome made from
4-
the product of two 2-digit numbers is 9009 = 91 x 99.
2+
Project Euler Problem 4: https://projecteuler.net/problem=4
53
6-
Find the largest palindrome made from the product of two 3-digit numbers which
7-
is less than N.
4+
Largest palindrome product
5+
6+
A palindromic number reads the same both ways. The largest palindrome made
7+
from the product of two 2-digit numbers is 9009 = 91 × 99.
8+
9+
Find the largest palindrome made from the product of two 3-digit numbers.
10+
11+
References:
12+
- https://en.wikipedia.org/wiki/Palindromic_number
813
"""
914

1015

1116
def solution(n: int = 998001) -> int:
12-
"""Returns the largest palindrome made from the product of two 3-digit
17+
"""
18+
Returns the largest palindrome made from the product of two 3-digit
1319
numbers which is less than n.
1420
1521
>>> solution(20000)
@@ -19,6 +25,7 @@ def solution(n: int = 998001) -> int:
1925
>>> solution(40000)
2026
39893
2127
"""
28+
2229
answer = 0
2330
for i in range(999, 99, -1): # 3 digit numbers range from 999 down to 100
2431
for j in range(999, 99, -1):
@@ -29,4 +36,4 @@ def solution(n: int = 998001) -> int:
2936

3037

3138
if __name__ == "__main__":
32-
print(solution(int(input().strip())))
39+
print(f"{solution() = }")

‎project_euler/problem_005/sol1.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
11
"""
2-
Problem:
3-
2520 is the smallest number that can be divided by each of the numbers from 1
4-
to 10 without any remainder.
2+
Project Euler Problem 5: https://projecteuler.net/problem=5
53
6-
What is the smallest positive number that is evenly divisible(divisible with no
7-
remainder) by all of the numbers from 1 to N?
4+
Smallest multiple
5+
6+
2520 is the smallest number that can be divided by each of the numbers
7+
from 1 to 10 without any remainder.
8+
9+
What is the smallest positive number that is _evenly divisible_ by all
10+
of the numbers from 1 to 20?
11+
12+
References:
13+
- https://en.wiktionary.org/wiki/evenly_divisible
814
"""
915

1016

1117
def solution(n: int = 20) -> int:
12-
"""Returns the smallest positive number that is evenly divisible(divisible
18+
"""
19+
Returns the smallest positive number that is evenly divisible (divisible
1320
with no remainder) by all of the numbers from 1 to n.
1421
1522
>>> solution(10)
1623
2520
1724
>>> solution(15)
1825
360360
19-
>>> solution(20)
20-
232792560
2126
>>> solution(22)
2227
232792560
2328
>>> solution(3.4)
2429
6
2530
>>> solution(0)
2631
Traceback (most recent call last):
2732
...
28-
ValueError: Parameter n must be greater or equal to one.
33+
ValueError: Parameter n must be greater than or equal to one.
2934
>>> solution(-17)
3035
Traceback (most recent call last):
3136
...
32-
ValueError: Parameter n must be greater or equal to one.
37+
ValueError: Parameter n must be greater than or equal to one.
3338
>>> solution([])
3439
Traceback (most recent call last):
3540
...
36-
TypeError: Parameter n must be int or passive of cast to int.
41+
TypeError: Parameter n must be int or castable to int.
3742
>>> solution("asd")
3843
Traceback (most recent call last):
3944
...
40-
TypeError: Parameter n must be int or passive of cast to int.
45+
TypeError: Parameter n must be int or castable to int.
4146
"""
47+
4248
try:
4349
n = int(n)
4450
except (TypeError, ValueError):
45-
raise TypeError("Parameter n must be int or passive of cast to int.")
51+
raise TypeError("Parameter n must be int or castable to int.")
4652
if n <= 0:
47-
raise ValueError("Parameter n must be greater or equal to one.")
53+
raise ValueError("Parameter n must be greater than or equal to one.")
4854
i = 0
4955
while 1:
5056
i += n * (n - 1)
@@ -60,4 +66,4 @@ def solution(n: int = 20) -> int:
6066

6167

6268
if __name__ == "__main__":
63-
print(solution(int(input().strip())))
69+
print(f"{solution() = }")

‎project_euler/problem_005/sol2.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,75 @@
11
"""
2-
Problem:
3-
2520 is the smallest number that can be divided by each of the numbers from 1
4-
to 10 without any remainder.
2+
Project Euler Problem 5: https://projecteuler.net/problem=5
53
6-
What is the smallest positive number that is evenly divisible(divisible with no
7-
remainder) by all of the numbers from 1 to N?
4+
Smallest multiple
5+
6+
2520 is the smallest number that can be divided by each of the numbers
7+
from 1 to 10 without any remainder.
8+
9+
What is the smallest positive number that is _evenly divisible_ by all
10+
of the numbers from 1 to 20?
11+
12+
References:
13+
- https://en.wiktionary.org/wiki/evenly_divisible
14+
- https://en.wikipedia.org/wiki/Euclidean_algorithm
15+
- https://en.wikipedia.org/wiki/Least_common_multiple
816
"""
9-
""" Euclidean GCD Algorithm """
1017

1118

1219
def gcd(x: int, y: int) -> int:
13-
return x if y == 0 else gcd(y, x % y)
20+
"""
21+
Euclidean GCD algorithm (Greatest Common Divisor)
1422
23+
>>> gcd(0, 0)
24+
0
25+
>>> gcd(23, 42)
26+
1
27+
>>> gcd(15, 33)
28+
3
29+
>>> gcd(12345, 67890)
30+
15
31+
"""
1532

16-
""" Using the property lcm*gcd of two numbers = product of them """
33+
return x if y == 0 else gcd(y, x % y)
1734

1835

1936
def lcm(x: int, y: int) -> int:
37+
"""
38+
Least Common Multiple.
39+
40+
Using the property that lcm(a, b) * gcd(a, b) = a*b
41+
42+
>>> lcm(3, 15)
43+
15
44+
>>> lcm(1, 27)
45+
27
46+
>>> lcm(13, 27)
47+
351
48+
>>> lcm(64, 48)
49+
192
50+
"""
51+
2052
return (x * y) // gcd(x, y)
2153

2254

2355
def solution(n: int = 20) -> int:
24-
"""Returns the smallest positive number that is evenly divisible(divisible
56+
"""
57+
Returns the smallest positive number that is evenly divisible (divisible
2558
with no remainder) by all of the numbers from 1 to n.
2659
2760
>>> solution(10)
2861
2520
2962
>>> solution(15)
3063
360360
31-
>>> solution(20)
32-
232792560
3364
>>> solution(22)
3465
232792560
3566
"""
67+
3668
g = 1
3769
for i in range(1, n + 1):
3870
g = lcm(g, i)
3971
return g
4072

4173

4274
if __name__ == "__main__":
43-
print(solution(int(input().strip())))
75+
print(f"{solution() = }")

‎project_euler/problem_006/sol1.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
"""
2-
Problem 6: https://projecteuler.net/problem=6
2+
Project Euler Problem 6: https://projecteuler.net/problem=6
3+
4+
Sum square difference
35
46
The sum of the squares of the first ten natural numbers is,
5-
1^2 + 2^2 + ... + 10^2 = 385
7+
1^2 + 2^2 + ... + 10^2 = 385
68
79
The square of the sum of the first ten natural numbers is,
8-
(1 + 2 + ... + 10)^2 = 552 = 3025
10+
(1 + 2 + ... + 10)^2 = 55^2 = 3025
911
10-
Hence the difference between the sum of the squares of the first ten natural
11-
numbers and the square of the sum is 3025 385 = 2640.
12+
Hence the difference between the sum of the squares of the first ten
13+
natural numbers and the square of the sum is 3025 - 385 = 2640.
1214
13-
Find the difference between the sum of the squares of the first N natural
14-
numbers and the square of the sum.
15+
Find the difference between the sum of the squares of the first one
16+
hundred natural numbers and the square of the sum.
1517
"""
1618

1719

1820
def solution(n: int = 100) -> int:
19-
"""Returns the difference between the sum of the squares of the first n
21+
"""
22+
Returns the difference between the sum of the squares of the first n
2023
natural numbers and the square of the sum.
2124
2225
>>> solution(10)
@@ -27,9 +30,8 @@ def solution(n: int = 100) -> int:
2730
41230
2831
>>> solution(50)
2932
1582700
30-
>>> solution()
31-
25164150
3233
"""
34+
3335
sum_of_squares = 0
3436
sum_of_ints = 0
3537
for i in range(1, n + 1):
@@ -39,7 +41,4 @@ def solution(n: int = 100) -> int:
3941

4042

4143
if __name__ == "__main__":
42-
import doctest
43-
44-
doctest.testmod()
45-
print(solution(int(input().strip())))
44+
print(f"{solution() = }")

‎project_euler/problem_006/sol2.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
"""
2-
Problem 6: https://projecteuler.net/problem=6
2+
Project Euler Problem 6: https://projecteuler.net/problem=6
3+
4+
Sum square difference
35
46
The sum of the squares of the first ten natural numbers is,
5-
1^2 + 2^2 + ... + 10^2 = 385
7+
1^2 + 2^2 + ... + 10^2 = 385
68
79
The square of the sum of the first ten natural numbers is,
8-
(1 + 2 + ... + 10)^2 = 552 = 3025
10+
(1 + 2 + ... + 10)^2 = 55^2 = 3025
911
10-
Hence the difference between the sum of the squares of the first ten natural
11-
numbers and the square of the sum is 3025 385 = 2640.
12+
Hence the difference between the sum of the squares of the first ten
13+
natural numbers and the square of the sum is 3025 - 385 = 2640.
1214
13-
Find the difference between the sum of the squares of the first N natural
14-
numbers and the square of the sum.
15+
Find the difference between the sum of the squares of the first one
16+
hundred natural numbers and the square of the sum.
1517
"""
1618

1719

1820
def solution(n: int = 100) -> int:
19-
"""Returns the difference between the sum of the squares of the first n
21+
"""
22+
Returns the difference between the sum of the squares of the first n
2023
natural numbers and the square of the sum.
2124
2225
>>> solution(10)
@@ -27,16 +30,12 @@ def solution(n: int = 100) -> int:
2730
41230
2831
>>> solution(50)
2932
1582700
30-
>>> solution()
31-
25164150
3233
"""
34+
3335
sum_cubes = (n * (n + 1) // 2) ** 2
3436
sum_squares = n * (n + 1) * (2 * n + 1) // 6
3537
return sum_cubes - sum_squares
3638

3739

3840
if __name__ == "__main__":
39-
import doctest
40-
41-
doctest.testmod()
42-
print(solution(int(input().strip())))
41+
print(f"{solution() = }")

‎project_euler/problem_006/sol3.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
"""
2-
Problem 6: https://projecteuler.net/problem=6
2+
Project Euler Problem 6: https://projecteuler.net/problem=6
3+
4+
Sum square difference
35
46
The sum of the squares of the first ten natural numbers is,
5-
1^2 + 2^2 + ... + 10^2 = 385
7+
1^2 + 2^2 + ... + 10^2 = 385
68
79
The square of the sum of the first ten natural numbers is,
8-
(1 + 2 + ... + 10)^2 = 552 = 3025
10+
(1 + 2 + ... + 10)^2 = 55^2 = 3025
911
10-
Hence the difference between the sum of the squares of the first ten natural
11-
numbers and the square of the sum is 3025 385 = 2640.
12+
Hence the difference between the sum of the squares of the first ten
13+
natural numbers and the square of the sum is 3025 - 385 = 2640.
1214
13-
Find the difference between the sum of the squares of the first N natural
14-
numbers and the square of the sum.
15+
Find the difference between the sum of the squares of the first one
16+
hundred natural numbers and the square of the sum.
1517
"""
1618
import math
1719

1820

1921
def solution(n: int = 100) -> int:
20-
"""Returns the difference between the sum of the squares of the first n
22+
"""
23+
Returns the difference between the sum of the squares of the first n
2124
natural numbers and the square of the sum.
2225
2326
>>> solution(10)
@@ -28,16 +31,12 @@ def solution(n: int = 100) -> int:
2831
41230
2932
>>> solution(50)
3033
1582700
31-
>>> solution()
32-
25164150
3334
"""
35+
3436
sum_of_squares = sum([i * i for i in range(1, n + 1)])
3537
square_of_sum = int(math.pow(sum(range(1, n + 1)), 2))
3638
return square_of_sum - sum_of_squares
3739

3840

3941
if __name__ == "__main__":
40-
import doctest
41-
42-
doctest.testmod()
43-
print(solution(int(input().strip())))
42+
print(f"{solution() = }")

‎project_euler/problem_006/sol4.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
"""
2-
Problem 6: https://projecteuler.net/problem=6
2+
Project Euler Problem 6: https://projecteuler.net/problem=6
3+
4+
Sum square difference
35
46
The sum of the squares of the first ten natural numbers is,
5-
1^2 + 2^2 + ... + 10^2 = 385
7+
1^2 + 2^2 + ... + 10^2 = 385
68
79
The square of the sum of the first ten natural numbers is,
8-
(1 + 2 + ... + 10)^2 = 552 = 3025
10+
(1 + 2 + ... + 10)^2 = 55^2 = 3025
911
10-
Hence the difference between the sum of the squares of the first ten natural
11-
numbers and the square of the sum is 3025 385 = 2640.
12+
Hence the difference between the sum of the squares of the first ten
13+
natural numbers and the square of the sum is 3025 - 385 = 2640.
1214
13-
Find the difference between the sum of the squares of the first N natural
14-
numbers and the square of the sum.
15+
Find the difference between the sum of the squares of the first one
16+
hundred natural numbers and the square of the sum.
1517
"""
1618

1719

1820
def solution(n: int = 100) -> int:
19-
"""Returns the difference between the sum of the squares of the first n
21+
"""
22+
Returns the difference between the sum of the squares of the first n
2023
natural numbers and the square of the sum.
2124
2225
>>> solution(10)
@@ -27,16 +30,12 @@ def solution(n: int = 100) -> int:
2730
41230
2831
>>> solution(50)
2932
1582700
30-
>>> solution()
31-
25164150
3233
"""
34+
3335
sum_of_squares = n * (n + 1) * (2 * n + 1) / 6
3436
square_of_sum = (n * (n + 1) / 2) ** 2
3537
return int(square_of_sum - sum_of_squares)
3638

3739

3840
if __name__ == "__main__":
39-
import doctest
40-
41-
doctest.testmod()
42-
print(solution(int(input("Enter a number: ").strip())))
41+
print(f"{solution() = }")

‎project_euler/problem_007/sol1.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
"""
2-
Problem 7: https://projecteuler.net/problem=7
2+
Project Euler Problem 7: https://projecteuler.net/problem=7
33
4-
By listing the first six prime numbers:
4+
10001st prime
55
6-
2, 3, 5, 7, 11, and 13
6+
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we
7+
can see that the 6th prime is 13.
78
8-
We can see that the 6th prime is 13. What is the Nth prime number?
9+
What is the 10001st prime number?
10+
11+
References:
12+
- https://en.wikipedia.org/wiki/Prime_number
913
"""
14+
1015
from math import sqrt
1116

1217

1318
def is_prime(num: int) -> bool:
14-
"""Determines whether the given number is prime or not"""
19+
"""
20+
Determines whether the given number is prime or not
21+
22+
>>> is_prime(2)
23+
True
24+
>>> is_prime(15)
25+
False
26+
>>> is_prime(29)
27+
True
28+
>>> is_prime(0)
29+
False
30+
"""
31+
1532
if num == 2:
1633
return True
1734
elif num % 2 == 0:
@@ -25,7 +42,8 @@ def is_prime(num: int) -> bool:
2542

2643

2744
def solution(nth: int = 10001) -> int:
28-
"""Returns the n-th prime number.
45+
"""
46+
Returns the n-th prime number.
2947
3048
>>> solution(6)
3149
13
@@ -39,9 +57,8 @@ def solution(nth: int = 10001) -> int:
3957
229
4058
>>> solution(100)
4159
541
42-
>>> solution()
43-
104743
4460
"""
61+
4562
count = 0
4663
number = 1
4764
while count != nth and number < 3:
@@ -56,4 +73,4 @@ def solution(nth: int = 10001) -> int:
5673

5774

5875
if __name__ == "__main__":
59-
print(solution(int(input().strip())))
76+
print(f"{solution() = }")

‎project_euler/problem_007/sol2.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
"""
2-
Problem 7: https://projecteuler.net/problem=7
2+
Project Euler Problem 7: https://projecteuler.net/problem=7
33
4-
By listing the first six prime numbers:
4+
10001st prime
55
6-
2, 3, 5, 7, 11, and 13
6+
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we
7+
can see that the 6th prime is 13.
78
8-
We can see that the 6th prime is 13. What is the Nth prime number?
9+
What is the 10001st prime number?
10+
11+
References:
12+
- https://en.wikipedia.org/wiki/Prime_number
913
"""
1014

1115

1216
def isprime(number: int) -> bool:
13-
"""Determines whether the given number is prime or not"""
17+
"""
18+
Determines whether the given number is prime or not
19+
20+
>>> isprime(2)
21+
True
22+
>>> isprime(15)
23+
False
24+
>>> isprime(29)
25+
True
26+
"""
27+
1428
for i in range(2, int(number ** 0.5) + 1):
1529
if number % i == 0:
1630
return False
1731
return True
1832

1933

2034
def solution(nth: int = 10001) -> int:
21-
"""Returns the n-th prime number.
35+
"""
36+
Returns the n-th prime number.
2237
2338
>>> solution(6)
2439
13
@@ -32,35 +47,32 @@ def solution(nth: int = 10001) -> int:
3247
229
3348
>>> solution(100)
3449
541
35-
>>> solution()
36-
104743
3750
>>> solution(3.4)
3851
5
3952
>>> solution(0)
4053
Traceback (most recent call last):
4154
...
42-
ValueError: Parameter nth must be greater or equal to one.
55+
ValueError: Parameter nth must be greater than or equal to one.
4356
>>> solution(-17)
4457
Traceback (most recent call last):
4558
...
46-
ValueError: Parameter nth must be greater or equal to one.
59+
ValueError: Parameter nth must be greater than or equal to one.
4760
>>> solution([])
4861
Traceback (most recent call last):
4962
...
50-
TypeError: Parameter nth must be int or passive of cast to int.
63+
TypeError: Parameter nth must be int or castable to int.
5164
>>> solution("asd")
5265
Traceback (most recent call last):
5366
...
54-
TypeError: Parameter nth must be int or passive of cast to int.
67+
TypeError: Parameter nth must be int or castable to int.
5568
"""
69+
5670
try:
5771
nth = int(nth)
5872
except (TypeError, ValueError):
59-
raise TypeError(
60-
"Parameter nth must be int or passive of cast to int."
61-
) from None
73+
raise TypeError("Parameter nth must be int or castable to int.") from None
6274
if nth <= 0:
63-
raise ValueError("Parameter nth must be greater or equal to one.")
75+
raise ValueError("Parameter nth must be greater than or equal to one.")
6476
primes = []
6577
num = 2
6678
while len(primes) < nth:
@@ -73,4 +85,4 @@ def solution(nth: int = 10001) -> int:
7385

7486

7587
if __name__ == "__main__":
76-
print(solution(int(input().strip())))
88+
print(f"{solution() = }")

‎project_euler/problem_007/sol3.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
"""
2-
Project 7: https://projecteuler.net/problem=7
2+
Project Euler Problem 7: https://projecteuler.net/problem=7
33
4-
By listing the first six prime numbers:
4+
10001st prime
55
6-
2, 3, 5, 7, 11, and 13
6+
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we
7+
can see that the 6th prime is 13.
78
8-
We can see that the 6th prime is 13. What is the Nth prime number?
9+
What is the 10001st prime number?
10+
11+
References:
12+
- https://en.wikipedia.org/wiki/Prime_number
913
"""
1014
import itertools
1115
import math
1216

1317

1418
def prime_check(number: int) -> bool:
15-
"""Determines whether a given number is prime or not"""
19+
"""
20+
Determines whether a given number is prime or not
21+
22+
>>> prime_check(2)
23+
True
24+
>>> prime_check(15)
25+
False
26+
>>> prime_check(29)
27+
True
28+
"""
29+
1630
if number % 2 == 0 and number > 2:
1731
return False
1832
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
1933

2034

2135
def prime_generator():
36+
"""
37+
Generate a sequence of prime numbers
38+
"""
39+
2240
num = 2
2341
while True:
2442
if prime_check(num):
@@ -27,7 +45,8 @@ def prime_generator():
2745

2846

2947
def solution(nth: int = 10001) -> int:
30-
"""Returns the n-th prime number.
48+
"""
49+
Returns the n-th prime number.
3150
3251
>>> solution(6)
3352
13
@@ -41,11 +60,9 @@ def solution(nth: int = 10001) -> int:
4160
229
4261
>>> solution(100)
4362
541
44-
>>> solution()
45-
104743
4663
"""
4764
return next(itertools.islice(prime_generator(), nth - 1, nth))
4865

4966

5067
if __name__ == "__main__":
51-
print(solution(int(input().strip())))
68+
print(f"{solution() = }")

‎project_euler/problem_008/sol1.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
"""
2-
Problem 8: https://projecteuler.net/problem=8
2+
Project Euler Problem 8: https://projecteuler.net/problem=8
3+
4+
Largest product in a series
35
46
The four adjacent digits in the 1000-digit number that have the greatest
57
product are 9 × 9 × 8 × 9 = 5832.
68
7-
73167176531330624919225119674426574742355349194934
8-
96983520312774506326239578318016984801869478851843
9-
85861560789112949495459501737958331952853208805511
10-
12540698747158523863050715693290963295227443043557
11-
66896648950445244523161731856403098711121722383113
12-
62229893423380308135336276614282806444486645238749
13-
30358907296290491560440772390713810515859307960866
14-
70172427121883998797908792274921901699720888093776
15-
65727333001053367881220235421809751254540594752243
16-
52584907711670556013604839586446706324415722155397
17-
53697817977846174064955149290862569321978468622482
18-
83972241375657056057490261407972968652414535100474
19-
82166370484403199890008895243450658541227588666881
20-
16427171479924442928230863465674813919123162824586
21-
17866458359124566529476545682848912883142607690042
22-
24219022671055626321111109370544217506941658960408
23-
07198403850962455444362981230987879927244284909188
24-
84580156166097919133875499200524063689912560717606
25-
05886116467109405077541002256983155200055935729725
26-
71636269561882670428252483600823257530420752963450
9+
73167176531330624919225119674426574742355349194934
10+
96983520312774506326239578318016984801869478851843
11+
85861560789112949495459501737958331952853208805511
12+
12540698747158523863050715693290963295227443043557
13+
66896648950445244523161731856403098711121722383113
14+
62229893423380308135336276614282806444486645238749
15+
30358907296290491560440772390713810515859307960866
16+
70172427121883998797908792274921901699720888093776
17+
65727333001053367881220235421809751254540594752243
18+
52584907711670556013604839586446706324415722155397
19+
53697817977846174064955149290862569321978468622482
20+
83972241375657056057490261407972968652414535100474
21+
82166370484403199890008895243450658541227588666881
22+
16427171479924442928230863465674813919123162824586
23+
17866458359124566529476545682848912883142607690042
24+
24219022671055626321111109370544217506941658960408
25+
07198403850962455444362981230987879927244284909188
26+
84580156166097919133875499200524063689912560717606
27+
05886116467109405077541002256983155200055935729725
28+
71636269561882670428252483600823257530420752963450
2729
2830
Find the thirteen adjacent digits in the 1000-digit number that have the
2931
greatest product. What is the value of this product?
3032
"""
33+
3134
import sys
3235

3336
N = """73167176531330624919225119674426574742355349194934\
@@ -53,12 +56,18 @@
5356

5457

5558
def solution(n: str = N) -> int:
56-
"""Find the thirteen adjacent digits in the 1000-digit number n that have
59+
"""
60+
Find the thirteen adjacent digits in the 1000-digit number n that have
5761
the greatest product and returns it.
5862
59-
>>> solution(N)
60-
23514624000
63+
>>> solution("13978431290823798458352374")
64+
609638400
65+
>>> solution("13978431295823798458352374")
66+
2612736000
67+
>>> solution("1397843129582379841238352374")
68+
209018880
6169
"""
70+
6271
largest_product = -sys.maxsize - 1
6372
for i in range(len(n) - 12):
6473
product = 1
@@ -70,4 +79,4 @@ def solution(n: str = N) -> int:
7079

7180

7281
if __name__ == "__main__":
73-
print(solution(N))
82+
print(f"{solution() = }")

‎project_euler/problem_008/sol2.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
"""
2-
Problem 8: https://projecteuler.net/problem=8
2+
Project Euler Problem 8: https://projecteuler.net/problem=8
3+
4+
Largest product in a series
35
46
The four adjacent digits in the 1000-digit number that have the greatest
57
product are 9 × 9 × 8 × 9 = 5832.
68
7-
73167176531330624919225119674426574742355349194934
8-
96983520312774506326239578318016984801869478851843
9-
85861560789112949495459501737958331952853208805511
10-
12540698747158523863050715693290963295227443043557
11-
66896648950445244523161731856403098711121722383113
12-
62229893423380308135336276614282806444486645238749
13-
30358907296290491560440772390713810515859307960866
14-
70172427121883998797908792274921901699720888093776
15-
65727333001053367881220235421809751254540594752243
16-
52584907711670556013604839586446706324415722155397
17-
53697817977846174064955149290862569321978468622482
18-
83972241375657056057490261407972968652414535100474
19-
82166370484403199890008895243450658541227588666881
20-
16427171479924442928230863465674813919123162824586
21-
17866458359124566529476545682848912883142607690042
22-
24219022671055626321111109370544217506941658960408
23-
07198403850962455444362981230987879927244284909188
24-
84580156166097919133875499200524063689912560717606
25-
05886116467109405077541002256983155200055935729725
26-
71636269561882670428252483600823257530420752963450
9+
73167176531330624919225119674426574742355349194934
10+
96983520312774506326239578318016984801869478851843
11+
85861560789112949495459501737958331952853208805511
12+
12540698747158523863050715693290963295227443043557
13+
66896648950445244523161731856403098711121722383113
14+
62229893423380308135336276614282806444486645238749
15+
30358907296290491560440772390713810515859307960866
16+
70172427121883998797908792274921901699720888093776
17+
65727333001053367881220235421809751254540594752243
18+
52584907711670556013604839586446706324415722155397
19+
53697817977846174064955149290862569321978468622482
20+
83972241375657056057490261407972968652414535100474
21+
82166370484403199890008895243450658541227588666881
22+
16427171479924442928230863465674813919123162824586
23+
17866458359124566529476545682848912883142607690042
24+
24219022671055626321111109370544217506941658960408
25+
07198403850962455444362981230987879927244284909188
26+
84580156166097919133875499200524063689912560717606
27+
05886116467109405077541002256983155200055935729725
28+
71636269561882670428252483600823257530420752963450
2729
2830
Find the thirteen adjacent digits in the 1000-digit number that have the
2931
greatest product. What is the value of this product?
3032
"""
31-
3233
from functools import reduce
3334

3435
N = (
@@ -56,12 +57,18 @@
5657

5758

5859
def solution(n: str = N) -> int:
59-
"""Find the thirteen adjacent digits in the 1000-digit number n that have
60+
"""
61+
Find the thirteen adjacent digits in the 1000-digit number n that have
6062
the greatest product and returns it.
6163
62-
>>> solution(N)
63-
23514624000
64+
>>> solution("13978431290823798458352374")
65+
609638400
66+
>>> solution("13978431295823798458352374")
67+
2612736000
68+
>>> solution("1397843129582379841238352374")
69+
209018880
6470
"""
71+
6572
return max(
6673
[
6774
reduce(lambda x, y: int(x) * int(y), n[i : i + 13])
@@ -71,4 +78,4 @@ def solution(n: str = N) -> int:
7178

7279

7380
if __name__ == "__main__":
74-
print(solution(str(N)))
81+
print(f"{solution() = }")

‎project_euler/problem_008/sol3.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
"""
2-
Problem 8: https://projecteuler.net/problem=8
2+
Project Euler Problem 8: https://projecteuler.net/problem=8
3+
4+
Largest product in a series
35
46
The four adjacent digits in the 1000-digit number that have the greatest
57
product are 9 × 9 × 8 × 9 = 5832.
68
7-
73167176531330624919225119674426574742355349194934
8-
96983520312774506326239578318016984801869478851843
9-
85861560789112949495459501737958331952853208805511
10-
12540698747158523863050715693290963295227443043557
11-
66896648950445244523161731856403098711121722383113
12-
62229893423380308135336276614282806444486645238749
13-
30358907296290491560440772390713810515859307960866
14-
70172427121883998797908792274921901699720888093776
15-
65727333001053367881220235421809751254540594752243
16-
52584907711670556013604839586446706324415722155397
17-
53697817977846174064955149290862569321978468622482
18-
83972241375657056057490261407972968652414535100474
19-
82166370484403199890008895243450658541227588666881
20-
16427171479924442928230863465674813919123162824586
21-
17866458359124566529476545682848912883142607690042
22-
24219022671055626321111109370544217506941658960408
23-
07198403850962455444362981230987879927244284909188
24-
84580156166097919133875499200524063689912560717606
25-
05886116467109405077541002256983155200055935729725
26-
71636269561882670428252483600823257530420752963450
9+
73167176531330624919225119674426574742355349194934
10+
96983520312774506326239578318016984801869478851843
11+
85861560789112949495459501737958331952853208805511
12+
12540698747158523863050715693290963295227443043557
13+
66896648950445244523161731856403098711121722383113
14+
62229893423380308135336276614282806444486645238749
15+
30358907296290491560440772390713810515859307960866
16+
70172427121883998797908792274921901699720888093776
17+
65727333001053367881220235421809751254540594752243
18+
52584907711670556013604839586446706324415722155397
19+
53697817977846174064955149290862569321978468622482
20+
83972241375657056057490261407972968652414535100474
21+
82166370484403199890008895243450658541227588666881
22+
16427171479924442928230863465674813919123162824586
23+
17866458359124566529476545682848912883142607690042
24+
24219022671055626321111109370544217506941658960408
25+
07198403850962455444362981230987879927244284909188
26+
84580156166097919133875499200524063689912560717606
27+
05886116467109405077541002256983155200055935729725
28+
71636269561882670428252483600823257530420752963450
2729
2830
Find the thirteen adjacent digits in the 1000-digit number that have the
2931
greatest product. What is the value of this product?
@@ -53,26 +55,27 @@
5355

5456

5557
def str_eval(s: str) -> int:
56-
"""Returns product of digits in given string n
58+
"""
59+
Returns product of digits in given string n
5760
5861
>>> str_eval("987654321")
5962
362880
6063
>>> str_eval("22222222")
6164
256
6265
"""
66+
6367
product = 1
6468
for digit in s:
6569
product *= int(digit)
6670
return product
6771

6872

6973
def solution(n: str = N) -> int:
70-
"""Find the thirteen adjacent digits in the 1000-digit number n that have
74+
"""
75+
Find the thirteen adjacent digits in the 1000-digit number n that have
7176
the greatest product and returns it.
72-
73-
>>> solution(N)
74-
23514624000
7577
"""
78+
7679
largest_product = -sys.maxsize - 1
7780
substr = n[:13]
7881
cur_index = 13
@@ -88,4 +91,4 @@ def solution(n: str = N) -> int:
8891

8992

9093
if __name__ == "__main__":
91-
print(solution(N))
94+
print(f"{solution() = }")

‎project_euler/problem_009/sol1.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
"""
2-
Problem 9: https://projecteuler.net/problem=9
2+
Project Euler Problem 9: https://projecteuler.net/problem=9
3+
4+
Special Pythagorean triplet
35
46
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
7+
58
a^2 + b^2 = c^2
9+
610
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
711
812
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
9-
Find the product abc.
13+
Find the product a*b*c.
14+
15+
References:
16+
- https://en.wikipedia.org/wiki/Pythagorean_triple
1017
"""
1118

1219

1320
def solution() -> int:
1421
"""
15-
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
16-
the following:
17-
1. a < b < c
18-
2. a**2 + b**2 = c**2
19-
3. a + b + c = 1000
22+
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
23+
the following:
24+
1. a < b < c
25+
2. a**2 + b**2 = c**2
26+
3. a + b + c = 1000
27+
2028
# The code below has been commented due to slow execution affecting Travis.
2129
# >>> solution()
2230
# 31875000
2331
"""
32+
2433
for a in range(300):
2534
for b in range(400):
2635
for c in range(500):
@@ -32,16 +41,17 @@ def solution() -> int:
3241

3342
def solution_fast() -> int:
3443
"""
35-
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
36-
the following:
37-
1. a < b < c
38-
2. a**2 + b**2 = c**2
39-
3. a + b + c = 1000
44+
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
45+
the following:
46+
1. a < b < c
47+
2. a**2 + b**2 = c**2
48+
3. a + b + c = 1000
4049
4150
# The code below has been commented due to slow execution affecting Travis.
4251
# >>> solution_fast()
4352
# 31875000
4453
"""
54+
4555
for a in range(300):
4656
for b in range(400):
4757
c = 1000 - a - b
@@ -66,4 +76,4 @@ def benchmark() -> None:
6676

6777

6878
if __name__ == "__main__":
69-
benchmark()
79+
print(f"{solution() = }")

‎project_euler/problem_009/sol2.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
"""
2-
Problem 9: https://projecteuler.net/problem=9
2+
Project Euler Problem 9: https://projecteuler.net/problem=9
3+
4+
Special Pythagorean triplet
35
46
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
7+
58
a^2 + b^2 = c^2
9+
610
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
711
812
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
9-
Find the product abc.
13+
Find the product a*b*c.
14+
15+
References:
16+
- https://en.wikipedia.org/wiki/Pythagorean_triple
1017
"""
1118

1219

1320
def solution(n: int = 1000) -> int:
1421
"""
15-
Return the product of a,b,c which are Pythagorean Triplet that satisfies
16-
the following:
17-
1. a < b < c
18-
2. a**2 + b**2 = c**2
19-
3. a + b + c = n
20-
21-
>>> solution(1000)
22-
31875000
22+
Return the product of a,b,c which are Pythagorean Triplet that satisfies
23+
the following:
24+
1. a < b < c
25+
2. a**2 + b**2 = c**2
26+
3. a + b + c = n
27+
28+
>>> solution(36)
29+
1620
30+
>>> solution(126)
31+
66780
2332
"""
33+
2434
product = -1
2535
candidate = 0
2636
for a in range(1, n // 3):
27-
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c"""
37+
# Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c
2838
b = (n * n - 2 * a * n) // (2 * n - 2 * a)
2939
c = n - a - b
3040
if c * c == (a * a + b * b):
@@ -35,4 +45,4 @@ def solution(n: int = 1000) -> int:
3545

3646

3747
if __name__ == "__main__":
38-
print(solution(int(input().strip())))
48+
print(f"{solution() = }")

‎project_euler/problem_009/sol3.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""
2-
Problem 9: https://projecteuler.net/problem=9
2+
Project Euler Problem 9: https://projecteuler.net/problem=9
3+
4+
Special Pythagorean triplet
35
46
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
57
@@ -8,22 +10,25 @@
810
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
911
1012
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
11-
Find the product abc.
13+
Find the product a*b*c.
14+
15+
References:
16+
- https://en.wikipedia.org/wiki/Pythagorean_triple
1217
"""
1318

1419

1520
def solution() -> int:
1621
"""
17-
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
18-
the following:
19-
20-
1. a**2 + b**2 = c**2
21-
2. a + b + c = 1000
22+
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
23+
the following:
24+
1. a**2 + b**2 = c**2
25+
2. a + b + c = 1000
2226
2327
# The code below has been commented due to slow execution affecting Travis.
2428
# >>> solution()
2529
# 31875000
2630
"""
31+
2732
return [
2833
a * b * (1000 - a - b)
2934
for a in range(1, 999)
@@ -33,4 +38,4 @@ def solution() -> int:
3338

3439

3540
if __name__ == "__main__":
36-
print(solution())
41+
print(f"{solution() = }")

‎project_euler/problem_010/sol1.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
"""
2-
https://projecteuler.net/problem=10
2+
Project Euler Problem 10: https://projecteuler.net/problem=10
3+
4+
Summation of primes
35
4-
Problem Statement:
56
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
67
78
Find the sum of all the primes below two million.
9+
10+
References:
11+
- https://en.wikipedia.org/wiki/Prime_number
812
"""
13+
914
from math import sqrt
1015

1116

1217
def is_prime(n: int) -> bool:
13-
"""Returns boolean representing primality of given number num.
18+
"""
19+
Returns boolean representing primality of given number num.
20+
1421
>>> is_prime(2)
1522
True
1623
>>> is_prime(3)
@@ -20,6 +27,7 @@ def is_prime(n: int) -> bool:
2027
>>> is_prime(2999)
2128
True
2229
"""
30+
2331
for i in range(2, int(sqrt(n)) + 1):
2432
if n % i == 0:
2533
return False
@@ -28,11 +36,9 @@ def is_prime(n: int) -> bool:
2836

2937

3038
def solution(n: int = 2000000) -> int:
31-
"""Returns the sum of all the primes below n.
39+
"""
40+
Returns the sum of all the primes below n.
3241
33-
# The code below has been commented due to slow execution affecting Travis.
34-
# >>> solution(2000000)
35-
# 142913828922
3642
>>> solution(1000)
3743
76127
3844
>>> solution(5000)
@@ -42,6 +48,7 @@ def solution(n: int = 2000000) -> int:
4248
>>> solution(7)
4349
10
4450
"""
51+
4552
if n > 2:
4653
sum_of_primes = 2
4754
else:
@@ -55,4 +62,4 @@ def solution(n: int = 2000000) -> int:
5562

5663

5764
if __name__ == "__main__":
58-
print(solution(int(input().strip())))
65+
print(f"{solution() = }")

‎project_euler/problem_010/sol2.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
"""
2-
https://projecteuler.net/problem=10
2+
Project Euler Problem 10: https://projecteuler.net/problem=10
3+
4+
Summation of primes
35
4-
Problem Statement:
56
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
67
78
Find the sum of all the primes below two million.
9+
10+
References:
11+
- https://en.wikipedia.org/wiki/Prime_number
812
"""
913
import math
1014
from itertools import takewhile
1115
from typing import Iterator
1216

1317

1418
def is_prime(number: int) -> bool:
15-
"""Returns boolean representing primality of given number num.
19+
"""
20+
Returns boolean representing primality of given number num.
21+
1622
>>> is_prime(2)
1723
True
1824
>>> is_prime(3)
@@ -22,12 +28,17 @@ def is_prime(number: int) -> bool:
2228
>>> is_prime(2999)
2329
True
2430
"""
31+
2532
if number % 2 == 0 and number > 2:
2633
return False
2734
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
2835

2936

3037
def prime_generator() -> Iterator[int]:
38+
"""
39+
Generate a list sequence of prime numbers
40+
"""
41+
3142
num = 2
3243
while True:
3344
if is_prime(num):
@@ -36,11 +47,9 @@ def prime_generator() -> Iterator[int]:
3647

3748

3849
def solution(n: int = 2000000) -> int:
39-
"""Returns the sum of all the primes below n.
50+
"""
51+
Returns the sum of all the primes below n.
4052
41-
# The code below has been commented due to slow execution affecting Travis.
42-
# >>> solution(2000000)
43-
# 142913828922
4453
>>> solution(1000)
4554
76127
4655
>>> solution(5000)
@@ -50,8 +59,9 @@ def solution(n: int = 2000000) -> int:
5059
>>> solution(7)
5160
10
5261
"""
62+
5363
return sum(takewhile(lambda x: x < n, prime_generator()))
5464

5565

5666
if __name__ == "__main__":
57-
print(solution(int(input().strip())))
67+
print(f"{solution() = }")

‎project_euler/problem_010/sol3.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
"""
2-
https://projecteuler.net/problem=10
2+
Project Euler Problem 10: https://projecteuler.net/problem=10
3+
4+
Summation of primes
35
4-
Problem Statement:
56
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
67
78
Find the sum of all the primes below two million.
9+
10+
References:
11+
- https://en.wikipedia.org/wiki/Prime_number
12+
- https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
813
"""
914

1015

1116
def solution(n: int = 2000000) -> int:
12-
"""Returns the sum of all the primes below n using Sieve of Eratosthenes:
17+
"""
18+
Returns the sum of all the primes below n using Sieve of Eratosthenes:
1319
14-
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
1520
The sieve of Eratosthenes is one of the most efficient ways to find all primes
1621
smaller than n when n is smaller than 10 million. Only for positive numbers.
1722
18-
>>> solution(2_000_000)
19-
142913828922
20-
>>> solution(1_000)
23+
>>> solution(1000)
2124
76127
22-
>>> solution(5_000)
25+
>>> solution(5000)
2326
1548136
24-
>>> solution(10_000)
27+
>>> solution(10000)
2528
5736396
2629
>>> solution(7)
2730
10
28-
>>> solution(7.1) # doctest: +ELLIPSIS
31+
>>> solution(7.1) # doctest: +ELLIPSIS
2932
Traceback (most recent call last):
3033
...
3134
TypeError: 'float' object cannot be interpreted as an integer
32-
>>> solution(-7) # doctest: +ELLIPSIS
35+
>>> solution(-7) # doctest: +ELLIPSIS
3336
Traceback (most recent call last):
3437
...
3538
IndexError: list assignment index out of range
36-
>>> solution("seven") # doctest: +ELLIPSIS
39+
>>> solution("seven") # doctest: +ELLIPSIS
3740
Traceback (most recent call last):
3841
...
3942
TypeError: can only concatenate str (not "int") to str
4043
"""
44+
4145
primality_list = [0 for i in range(n + 1)]
4246
primality_list[0] = 1
4347
primality_list[1] = 1
@@ -54,4 +58,4 @@ def solution(n: int = 2000000) -> int:
5458

5559

5660
if __name__ == "__main__":
57-
print(solution(int(input().strip())))
61+
print(f"{solution() = }")

0 commit comments

Comments
 (0)
Please sign in to comment.