-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem78.py
More file actions
36 lines (32 loc) · 914 Bytes
/
problem78.py
File metadata and controls
36 lines (32 loc) · 914 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
"""
Coin Partitions
Project Euler Problem #78
by Muaz Siddiqui
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?
"""
from euler_helpers import timeit
@timeit
def answer():
# Generating Integer Partitions with pentagonal numbers
pentagonals = sum([[i*(3*i - 1)/2, i*(3*i - 1)/2 + i] for i in range(1, 200)], [])
# Sign flip flops every two terms
sign = [1,1,-1,-1]
limit = 1000000
partition = [1]
index = 0
while True:
index += 1
last, i = 0, 0
while pentagonals[i] <= index:
last += partition[int(index - pentagonals[i])] * sign[i % 4]
i += 1
partition.append(last % limit)
if partition[index] == 0:
return index