forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp077.py
More file actions
35 lines (26 loc) · 716 Bytes
/
p077.py
File metadata and controls
35 lines (26 loc) · 716 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
#
# Solution to Project Euler problem 77
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib, itertools, sys
if sys.version_info.major == 2:
filter = itertools.ifilter
def compute():
cond = lambda n: num_prime_sum_ways(n) > 5000
ans = next(filter(cond, itertools.count(2)))
return str(ans)
primes = [2]
def num_prime_sum_ways(n):
for i in range(primes[-1] + 1, n + 1):
if eulerlib.is_prime(i):
primes.append(i)
ways = [1] + [0] * n
for p in primes:
for i in range(n + 1 - p):
ways[i + p] += ways[i]
return ways[n]
if __name__ == "__main__":
print(compute())