-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp077.py
More file actions
executable file
·55 lines (46 loc) · 1.15 KB
/
p077.py
File metadata and controls
executable file
·55 lines (46 loc) · 1.15 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
49
50
51
52
53
54
# -*- 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?
import logging
from util import *
def prime_sum(n, prime_numbers):
if (n < 2): return 0
prime_lt_n = []
for p in prime_numbers:
if (p > n) : break
prime_lt_n.append(p)
prime_lt_n.reverse()
return find_sum(n, prime_lt_n)
def find_sum(n, a):
if (n < a[-1]): return 0
if (len(a) == 1):
if (n % a[0] == 0):
return 1
return 0
b = a[1:]
d = n//a[0]
sum = 0
for i in range(d+1):
sum += find_sum(n-i*a[0], b)
if (n % a[0] == 0): sum += 1
return sum
def main(args):
if args.test:
L = 10+1
else:
L = 100
prime = PrimeNumberPool(L)
for n in range(L//2, L):
ps = prime_sum(n, prime.numbers)
logging.debug("{} {}".format(n, ps))
if ps > 5000:
logging.info("answer: {}".format(n))
logging.debug(ps)
return 0