From 329feb492843129a285e8ec11b9d0c07c28579ba Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Wed, 27 Oct 2021 14:49:04 +0530 Subject: [PATCH] Add Project Euler Problem 078 solution 01 (#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 * 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 --- DIRECTORY.md | 2 + project_euler/problem_078/__init__.py | 0 project_euler/problem_078/sol1.py | 55 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 project_euler/problem_078/__init__.py create mode 100644 project_euler/problem_078/sol1.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 6fbd5e2cc..c94fb78d6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -788,6 +788,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_076/sol1.py) * Problem 077 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_077/sol1.py) + * Problem 078 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_078/sol1.py) * Problem 080 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py) * Problem 081 diff --git a/project_euler/problem_078/__init__.py b/project_euler/problem_078/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py new file mode 100644 index 000000000..f92cf0f40 --- /dev/null +++ b/project_euler/problem_078/sol1.py @@ -0,0 +1,55 @@ +""" +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() + 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 + 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() = }")