-
Notifications
You must be signed in to change notification settings - Fork 0
/
078.py
78 lines (66 loc) · 1.44 KB
/
078.py
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
72
73
74
75
76
77
78
#!python
"""
Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so p(5)=7.
OOOOO
OOOO O
OOO OO
OOO O O
OO OO O
OO O O O
O O O O O
Find the least value of n for which p(n) is divisible by one million.
"""
from math import factorial
from functools import lru_cache
def choose(n, r):
return int(factorial(n)/(factorial(r)*factorial(n-r)))
@lru_cache(maxsize=4096)
def n_terms_sum_to_a_count(a, n, m):
#print([a, n, m])
if n==1:
if a <= m and a>0:
return 1
return 0
collection = 0
for i in range(1,min([a, m+1])):
#print([a,n,i])
collection+= n_terms_sum_to_a_count(a-i, n-1, min([m, i]))
return collection
def old_p(n):
c=0
for i in range(1,n+1):
c+= n_terms_sum_to_a_count(n,i,n)
return c
def nth_pentagonal(n):
return int(n*(3*n-1)/2)
def gen_gen_pentagonal(n):
#yield nth_pentagonal(0)
for i in range(1,n):
p = nth_pentagonal(i)
if p >= n:
return
yield p
p = nth_pentagonal(-i)
if p > n:
return
yield p
def p(k):
return p_helper(k+1)
@lru_cache(maxsize=None)
def p_helper(k):
if k<0:
return 0
if k==1:
return 1
rval = 0
signs = [1, 1, -1, -1 ]
sign_idx = 0
for pent in gen_gen_pentagonal(k+1):
rval += signs[sign_idx]*p_helper(k-pent)
sign_idx = (sign_idx+1)%4
return rval
for n in range(1,10**6):
pp = p(n)
print([n, pp])
if pp%1000000==0:
exit()