Skip to content

Commit 329feb4

Browse files
FirePing32github-actionspoyea
authored
Add Project Euler Problem 078 solution 01 (TheAlgorithms#5565)
* Create sol1.py * updating DIRECTORY.md * Create __init__.py * Add docstring * Reformat with black * Fix flake8 issues * Add EOL * Fix formatting issues * Add docstring * Add func return type * Change return type * Remove test print statement * Reformat code * Fix return types * Break loop * Update doctest sol * Update project_euler/problem_078/sol1.py Co-authored-by: John Law <[email protected]> * Added doctest and changed return type * Add int() * Fix flake8 issues * Use argument instead of fixed constant * Update sol1.py * fix sol1.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: John Law <[email protected]>
1 parent ce9a139 commit 329feb4

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,8 @@
788788
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_076/sol1.py)
789789
* Problem 077
790790
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_077/sol1.py)
791+
* Problem 078
792+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_078/sol1.py)
791793
* Problem 080
792794
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py)
793795
* Problem 081

project_euler/problem_078/__init__.py

Whitespace-only changes.

project_euler/problem_078/sol1.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Problem 78
3+
Url: https://projecteuler.net/problem=78
4+
Statement:
5+
Let p(n) represent the number of different ways in which n coins
6+
can be separated into piles. For example, five coins can be separated
7+
into piles in exactly seven different ways, so p(5)=7.
8+
9+
OOOOO
10+
OOOO O
11+
OOO OO
12+
OOO O O
13+
OO OO O
14+
OO O O O
15+
O O O O O
16+
Find the least value of n for which p(n) is divisible by one million.
17+
"""
18+
19+
import itertools
20+
21+
22+
def solution(number: int = 1000000) -> int:
23+
"""
24+
>>> solution()
25+
55374
26+
"""
27+
partitions = [1]
28+
29+
for i in itertools.count(len(partitions)):
30+
item = 0
31+
for j in itertools.count(1):
32+
sign = -1 if j % 2 == 0 else +1
33+
index = (j * j * 3 - j) // 2
34+
if index > i:
35+
break
36+
item += partitions[i - index] * sign
37+
index += j
38+
if index > i:
39+
break
40+
item += partitions[i - index] * sign
41+
item %= number
42+
43+
if item == 0:
44+
return i
45+
partitions.append(item)
46+
47+
return 0
48+
49+
50+
if __name__ == "__main__":
51+
import doctest
52+
53+
doctest.testmod()
54+
55+
print(f"{solution() = }")

0 commit comments

Comments
 (0)