-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp078.py
More file actions
71 lines (60 loc) · 1.48 KB
/
Copy pathp078.py
File metadata and controls
71 lines (60 loc) · 1.48 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from euler import timeit
def pentagonal():
k = 1
while True:
yield (k, (k*(3*k - 1))//2)
yield (-k, (k*(3*k + 1))//2)
k += 1
@timeit
def main():
p = [1, 1]
n = 2
while True:
t = 0
for k, n0 in pentagonal():
if n0 > n:
break
t += int((-1)**(k-1))*p[n-n0]
p.append(t)
if t % 1000000 == 0:
print('p({}) = {}'.format(len(p)-1, t))
break
n += 1
# for n, pn in enumerate(p):
# print('p({}) = {}'.format(n, pn))
@timeit
def solve_euler():
p = [1, 1]
N = 1000000
n = 1
while p[n] != 0:
n += 1
i = 0
t = 0 # temp variable to accumulate p[n]
while True:
i += 1
m1 = n - (i*(3*i - 1))//2
m2 = n - (i*(3*i + 1))//2
s = 1
if i % 2 == 0: s = -1
if m1 >= 0: t += s * p[m1]
if m2 >= 0: t += s * p[m2]
if m1 < 0 and m2 < 0:
break
t = t % N
p.append(t)
print('PE78 Ans: {}'.format(n))
if __name__ == '__main__':
main()
solve_euler()
# First try - wrong logic
# if __name__ == "__main__":
# limit = 10**5
# T = 10**6
# partitions = [1]+[0]*limit
# for k in range(1, limit+1):
# for n in range(k, limit+1):
# partitions[n] += partitions[n-k]
# if partitions[k] % T == 0:
# print("PE78 Ans: %d" % k)
# break