-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob0010.py
More file actions
48 lines (39 loc) · 1.33 KB
/
Copy pathprob0010.py
File metadata and controls
48 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Project Euler Problem 10. https://projecteuler.net/problem=10
"""
def prime_generator():
"""
This is the sieve of Erosthenes, as implemented by David Eppstein, UC Irvine, 28 Feb 2002
For the method of calculating primes that I came up with myself, see my solution to problem 7
:return: the next prime number
"""
# map of composites (key) with at least one prime factor in list as value
D = {}
# first number to test if prime
q = 2
while 1:
if q not in D:
# next prime found
yield q
# add it's square as a composite to D
D[q**2] = [q]
else:
# update dictionary entries based on composite and its listed primes
for p in D[q]:
D.setdefault(p+q, []).append(p)
del D[q]
q += 1
def prime_sum(max_value)->int:
"""
Sums all prime numbers up to the specified value. Returns the sum.
:param max_value: primes up to (and including, if prime) are added
:return: the sum of primes up to max_value
"""
prime_gen = prime_generator()
current_prime = next(prime_gen)
total = 0
while current_prime < max_value:
total += current_prime
current_prime = next(prime_gen)
return total
print(prime_sum(2000000))