-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathsol1.py
68 lines (57 loc) · 1.08 KB
/
sol1.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Prime summations
Problem 77
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 solution(length=5000) -> int:
"""Ways to write sum of primes
Keyword arguments:
amount -- amount of different ways (default 5000)
Passing no parameters returns the result
>>> solution()
71
"""
primes = [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
]
target = 11
while True:
ways = [1] + [0] * target
for p in primes:
for i in range(p, target + 1):
ways[i] += ways[i - p]
if ways[target] > length:
break
target += 1
return target
if __name__ == "__main__":
print(solution())