Skip to content

Commit e74adc4

Browse files
authored
Project Euler Problem 10: style improvements (#2924)
Rename the main solution functions to solution. Rename prime chec functions to is_prime. Add default args, typehints, expand variable names.
1 parent 000cedc commit e74adc4

File tree

3 files changed

+68
-48
lines changed

3 files changed

+68
-48
lines changed

Diff for: project_euler/problem_10/sol1.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
https://projecteuler.net/problem=10
3+
24
Problem Statement:
35
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
46
@@ -7,28 +9,25 @@
79
from math import sqrt
810

911

10-
def is_prime(n):
12+
def is_prime(n: int) -> bool:
13+
"""Returns boolean representing primality of given number num.
14+
>>> is_prime(2)
15+
True
16+
>>> is_prime(3)
17+
True
18+
>>> is_prime(27)
19+
False
20+
>>> is_prime(2999)
21+
True
22+
"""
1123
for i in range(2, int(sqrt(n)) + 1):
1224
if n % i == 0:
1325
return False
1426

1527
return True
1628

1729

18-
def sum_of_primes(n):
19-
if n > 2:
20-
sumOfPrimes = 2
21-
else:
22-
return 0
23-
24-
for i in range(3, n, 2):
25-
if is_prime(i):
26-
sumOfPrimes += i
27-
28-
return sumOfPrimes
29-
30-
31-
def solution(n):
30+
def solution(n: int = 2000000) -> int:
3231
"""Returns the sum of all the primes below n.
3332
3433
# The code below has been commented due to slow execution affecting Travis.
@@ -43,7 +42,16 @@ def solution(n):
4342
>>> solution(7)
4443
10
4544
"""
46-
return sum_of_primes(n)
45+
if n > 2:
46+
sum_of_primes = 2
47+
else:
48+
return 0
49+
50+
for i in range(3, n, 2):
51+
if is_prime(i):
52+
sum_of_primes += i
53+
54+
return sum_of_primes
4755

4856

4957
if __name__ == "__main__":

Diff for: project_euler/problem_10/sol2.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
"""
2+
https://projecteuler.net/problem=10
3+
24
Problem Statement:
35
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
46
57
Find the sum of all the primes below two million.
68
"""
79
import math
810
from itertools import takewhile
9-
10-
11-
def primeCheck(number):
11+
from typing import Iterator
12+
13+
14+
def is_prime(number: int) -> bool:
15+
"""Returns boolean representing primality of given number num.
16+
>>> is_prime(2)
17+
True
18+
>>> is_prime(3)
19+
True
20+
>>> is_prime(27)
21+
False
22+
>>> is_prime(2999)
23+
True
24+
"""
1225
if number % 2 == 0 and number > 2:
1326
return False
1427
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
1528

1629

17-
def prime_generator():
30+
def prime_generator() -> Iterator[int]:
1831
num = 2
1932
while True:
20-
if primeCheck(num):
33+
if is_prime(num):
2134
yield num
2235
num += 1
2336

2437

25-
def solution(n):
38+
def solution(n: int = 2000000) -> int:
2639
"""Returns the sum of all the primes below n.
2740
2841
# The code below has been commented due to slow execution affecting Travis.

Diff for: project_euler/problem_10/sol3.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,54 @@
44
Problem Statement:
55
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
66
7-
Find the sum of all the primes below two million using Sieve_of_Eratosthenes:
8-
9-
The sieve of Eratosthenes is one of the most efficient ways to find all primes
10-
smaller than n when n is smaller than 10 million. Only for positive numbers.
7+
Find the sum of all the primes below two million.
118
"""
129

1310

14-
def prime_sum(n: int) -> int:
15-
"""Returns the sum of all the primes below n.
11+
def solution(n: int = 2000000) -> int:
12+
"""Returns the sum of all the primes below n using Sieve of Eratosthenes:
13+
14+
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
15+
The sieve of Eratosthenes is one of the most efficient ways to find all primes
16+
smaller than n when n is smaller than 10 million. Only for positive numbers.
1617
17-
>>> prime_sum(2_000_000)
18+
>>> solution(2_000_000)
1819
142913828922
19-
>>> prime_sum(1_000)
20+
>>> solution(1_000)
2021
76127
21-
>>> prime_sum(5_000)
22+
>>> solution(5_000)
2223
1548136
23-
>>> prime_sum(10_000)
24+
>>> solution(10_000)
2425
5736396
25-
>>> prime_sum(7)
26+
>>> solution(7)
2627
10
27-
>>> prime_sum(7.1) # doctest: +ELLIPSIS
28+
>>> solution(7.1) # doctest: +ELLIPSIS
2829
Traceback (most recent call last):
2930
...
3031
TypeError: 'float' object cannot be interpreted as an integer
31-
>>> prime_sum(-7) # doctest: +ELLIPSIS
32+
>>> solution(-7) # doctest: +ELLIPSIS
3233
Traceback (most recent call last):
3334
...
3435
IndexError: list assignment index out of range
35-
>>> prime_sum("seven") # doctest: +ELLIPSIS
36+
>>> solution("seven") # doctest: +ELLIPSIS
3637
Traceback (most recent call last):
3738
...
3839
TypeError: can only concatenate str (not "int") to str
3940
"""
40-
list_ = [0 for i in range(n + 1)]
41-
list_[0] = 1
42-
list_[1] = 1
41+
primality_list = [0 for i in range(n + 1)]
42+
primality_list[0] = 1
43+
primality_list[1] = 1
4344

4445
for i in range(2, int(n ** 0.5) + 1):
45-
if list_[i] == 0:
46+
if primality_list[i] == 0:
4647
for j in range(i * i, n + 1, i):
47-
list_[j] = 1
48-
s = 0
48+
primality_list[j] = 1
49+
sum_of_primes = 0
4950
for i in range(n):
50-
if list_[i] == 0:
51-
s += i
52-
return s
51+
if primality_list[i] == 0:
52+
sum_of_primes += i
53+
return sum_of_primes
5354

5455

5556
if __name__ == "__main__":
56-
# import doctest
57-
# doctest.testmod()
58-
print(prime_sum(int(input().strip())))
57+
print(solution(int(input().strip())))

0 commit comments

Comments
 (0)