|
4 | 4 | Problem Statement:
|
5 | 5 | The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
6 | 6 |
|
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. |
11 | 8 | """
|
12 | 9 |
|
13 | 10 |
|
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. |
16 | 17 |
|
17 |
| - >>> prime_sum(2_000_000) |
| 18 | + >>> solution(2_000_000) |
18 | 19 | 142913828922
|
19 |
| - >>> prime_sum(1_000) |
| 20 | + >>> solution(1_000) |
20 | 21 | 76127
|
21 |
| - >>> prime_sum(5_000) |
| 22 | + >>> solution(5_000) |
22 | 23 | 1548136
|
23 |
| - >>> prime_sum(10_000) |
| 24 | + >>> solution(10_000) |
24 | 25 | 5736396
|
25 |
| - >>> prime_sum(7) |
| 26 | + >>> solution(7) |
26 | 27 | 10
|
27 |
| - >>> prime_sum(7.1) # doctest: +ELLIPSIS |
| 28 | + >>> solution(7.1) # doctest: +ELLIPSIS |
28 | 29 | Traceback (most recent call last):
|
29 | 30 | ...
|
30 | 31 | TypeError: 'float' object cannot be interpreted as an integer
|
31 |
| - >>> prime_sum(-7) # doctest: +ELLIPSIS |
| 32 | + >>> solution(-7) # doctest: +ELLIPSIS |
32 | 33 | Traceback (most recent call last):
|
33 | 34 | ...
|
34 | 35 | IndexError: list assignment index out of range
|
35 |
| - >>> prime_sum("seven") # doctest: +ELLIPSIS |
| 36 | + >>> solution("seven") # doctest: +ELLIPSIS |
36 | 37 | Traceback (most recent call last):
|
37 | 38 | ...
|
38 | 39 | TypeError: can only concatenate str (not "int") to str
|
39 | 40 | """
|
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 |
43 | 44 |
|
44 | 45 | for i in range(2, int(n ** 0.5) + 1):
|
45 |
| - if list_[i] == 0: |
| 46 | + if primality_list[i] == 0: |
46 | 47 | 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 |
49 | 50 | 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 |
53 | 54 |
|
54 | 55 |
|
55 | 56 | 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