-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 77
More file actions
45 lines (39 loc) · 951 Bytes
/
Copy pathproblem 77
File metadata and controls
45 lines (39 loc) · 951 Bytes
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
"""It is possible to write ten as the sum of primes in exactly five different ways:
7 + 3
5 + 5
5 + 3 + 2
3 + 3 + 2 + 2
2 + 2 + 2 + 2 + 2
What is the first value which can be written as the sum of primes in over five thousand different ways?"""
def all_primes(upper_bound):
primes = [2]
for i in range(3, upper_bound):
j = 0
while j < len(primes) and primes[j] <= int(i ** 0.5):
if i % primes[j] == 0:
break
j += 1
else:
primes.append(i)
return primes
def is_prime(x, primes):
sqrt = int(x ** 0.5) + 1
for prime in primes:
if x % prime == 0:
return False
if prime == primes[-1]:
return False
if prime > sqrt:
return True
return True
primes = []
primes = all_primes(100)
#print(primes)
ways = [0] * 101
ways[0] = 1
for x in primes:
for i in range(x, 101):
ways[i] += ways[i - x]
if ways[i] > 5000 :
print(i, ways[i])
break