forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol1.py
62 lines (49 loc) · 1.3 KB
/
sol1.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
"""
Problem 78
Url: https://projecteuler.net/problem=78
Statement:
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.
"""
import itertools
def solution(number: int = 1000000) -> int:
"""
>>> solution(1)
1
>>> solution(9)
14
>>> solution()
55374
"""
partitions = [1]
for i in itertools.count(len(partitions)):
item = 0
for j in itertools.count(1):
sign = -1 if j % 2 == 0 else +1
index = (j * j * 3 - j) // 2
if index > i:
break
item += partitions[i - index] * sign
item %= number
index += j
if index > i:
break
item += partitions[i - index] * sign
item %= number
if item == 0:
return i
partitions.append(item)
return 0
if __name__ == "__main__":
import doctest
doctest.testmod()
print(f"{solution() = }")