-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path077.py
More file actions
executable file
·48 lines (39 loc) · 942 Bytes
/
Copy path077.py
File metadata and controls
executable file
·48 lines (39 loc) · 942 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
46
47
48
#!/usr/bin/python
# -*- coding: utf-8 -*-
#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?
#Answer:
#71
from time import time; t=time()
from mathplus import memorize, get_primes_by_sieve, takewhile
M = 100
L = 5000
primes, sieves = get_primes_by_sieve(M)
flag = 0
for i in range(M):
if sieves[i]: flag = i
sieves[i] = flag
@memorize
def sp(n, m):
if n == 0: return 1
if n <= 1 or m <= 1: return 0
if n == 2: return 1
s = 0
for i in takewhile(lambda x: x <= m, primes):
ni = n - i
if i <= ni:
s += sp(ni, i)
else:
s += sp(ni, sieves[ni])
return s
test = 0
while True:
v = sp(test, sieves[test])
if v > L: break
test += 1
print(test)#, time()-t