From 356bc6c4ff53aa7c1926049568e7815581db02a2 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 18:26:15 +0530 Subject: [PATCH 01/23] Create sol1.py --- project_euler/problem_078/sol1.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 project_euler/problem_078/sol1.py diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py new file mode 100644 index 000000000000..c9e7f061a5ed --- /dev/null +++ b/project_euler/problem_078/sol1.py @@ -0,0 +1,46 @@ +""" +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(): + MODULUS = 10 ** 6 + 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 # index == (j * j * 3 + j) // 2 + if index > i: + break + item += partitions[i - index] * sign + item %= MODULUS + + # Check or memoize the number + + if item == 0: + return str(i) + partitions.append(item) + + +if __name__ == '__main__': + print(solution()) From 63b5438d92948d54d7ebff9925a640ec70c8d2f2 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 23 Oct 2021 12:56:33 +0000 Subject: [PATCH 02/23] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 950d8e2c0c4b..22dbae2f4007 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -773,6 +773,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 From 23a6802e21ff956fee432b076964fc8b32d27783 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 18:27:18 +0530 Subject: [PATCH 03/23] Create __init__.py --- project_euler/problem_078/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_078/__init__.py diff --git a/project_euler/problem_078/__init__.py b/project_euler/problem_078/__init__.py new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/project_euler/problem_078/__init__.py @@ -0,0 +1 @@ + From ece0d19a67a2b171914d92eb6f307daf012c9187 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 20:20:43 +0530 Subject: [PATCH 04/23] Add docstring --- project_euler/problem_078/sol1.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index c9e7f061a5ed..58d297473dbe 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -18,6 +18,10 @@ def solution(): + """ + >>> solution() + 55374 + """ MODULUS = 10 ** 6 partitions = [1] for i in itertools.count(len(partitions)): From 17746942e5db6ed54d75ea8a1bf282ea631ccccb Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 20:44:24 +0530 Subject: [PATCH 05/23] Reformat with black --- project_euler/problem_078/sol1.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 58d297473dbe..101ba2e050d1 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -18,17 +18,13 @@ def solution(): - """ - >>> solution() - 55374 - """ MODULUS = 10 ** 6 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) + sign = -1 if j % 2 == 0 else +1 index = (j * j * 3 - j) // 2 if index > i: break @@ -46,5 +42,5 @@ def solution(): partitions.append(item) -if __name__ == '__main__': +if __name__ == "__main__": print(solution()) From 6ad008fb8f298ffd55fbd4a67cb9143b049ba1ba Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 22:21:07 +0530 Subject: [PATCH 06/23] Fix flake8 issues --- project_euler/problem_078/sol1.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 101ba2e050d1..2cb249dc781d 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -2,7 +2,9 @@ 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. +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 From e193bfbffcc82e496ba45417a13b3f0c8ba10695 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 22:29:32 +0530 Subject: [PATCH 07/23] Add EOL --- project_euler/problem_078/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 2cb249dc781d..3481b67b781f 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -46,3 +46,4 @@ def solution(): if __name__ == "__main__": print(solution()) + From 5dc43f5c36cf0951d1280b6930cb4ab18db55c4c Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 22:32:00 +0530 Subject: [PATCH 08/23] Fix formatting issues --- project_euler/problem_078/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 3481b67b781f..2cb249dc781d 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -46,4 +46,3 @@ def solution(): if __name__ == "__main__": print(solution()) - From 19373b31818e4927cd1c90a5b771473022579f35 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 22:48:35 +0530 Subject: [PATCH 09/23] Add docstring --- project_euler/problem_078/sol1.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 2cb249dc781d..8041f75e2005 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -20,6 +20,10 @@ def solution(): + """ + >>> solution() + 55374 + """ MODULUS = 10 ** 6 partitions = [1] for i in itertools.count(len(partitions)): From b78fb541dec19e0eaa3046818e74d8818c3e8d1e Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 22:51:32 +0530 Subject: [PATCH 10/23] Add func return type --- project_euler/problem_078/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 8041f75e2005..95324517bc4a 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -19,7 +19,7 @@ import itertools -def solution(): +def solution() -> int: """ >>> solution() 55374 From e5a77b1dd737d8be17ff1fc73c9d49fc47856df7 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 23:15:24 +0530 Subject: [PATCH 11/23] Change return type --- project_euler/problem_078/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 95324517bc4a..b3ace5088b2d 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -19,7 +19,7 @@ import itertools -def solution() -> int: +def solution() -> str: """ >>> solution() 55374 From 87b6f67d5b543fe91209b1c6d2d298fed475fd09 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sat, 23 Oct 2021 23:23:35 +0530 Subject: [PATCH 12/23] Remove test print statement --- project_euler/problem_078/sol1.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index b3ace5088b2d..a607a227111b 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -46,7 +46,3 @@ def solution() -> str: if item == 0: return str(i) partitions.append(item) - - -if __name__ == "__main__": - print(solution()) From b25d844408668ea56ee5952cafdc923f9033a52d Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sun, 24 Oct 2021 13:53:15 +0530 Subject: [PATCH 13/23] Reformat code --- project_euler/problem_078/__init__.py | 1 - project_euler/problem_078/sol1.py | 12 +++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_078/__init__.py b/project_euler/problem_078/__init__.py index 8b137891791f..e69de29bb2d1 100644 --- a/project_euler/problem_078/__init__.py +++ b/project_euler/problem_078/__init__.py @@ -1 +0,0 @@ - diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index a607a227111b..95b84f36503b 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -19,13 +19,14 @@ import itertools -def solution() -> str: +def solution() -> int: """ >>> solution() 55374 """ MODULUS = 10 ** 6 partitions = [1] + result = "" for i in itertools.count(len(partitions)): item = 0 @@ -35,14 +36,15 @@ def solution() -> str: if index > i: break item += partitions[i - index] * sign - index += j # index == (j * j * 3 + j) // 2 + index += j if index > i: break item += partitions[i - index] * sign item %= MODULUS - # Check or memoize the number + partitions.append(item) if item == 0: - return str(i) - partitions.append(item) + result = i + + return result From f8b3136b6bc270e1f2d4473601e834717c330b5e Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sun, 24 Oct 2021 13:57:53 +0530 Subject: [PATCH 14/23] Fix return types --- project_euler/problem_078/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 95b84f36503b..f061efcaf8c5 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -19,7 +19,7 @@ import itertools -def solution() -> int: +def solution() -> str: """ >>> solution() 55374 @@ -45,6 +45,6 @@ def solution() -> int: partitions.append(item) if item == 0: - result = i + result = str(i) return result From d2bf43d9271f9ad5443d31faf37972b9b2eaa220 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sun, 24 Oct 2021 15:00:54 +0530 Subject: [PATCH 15/23] Break loop --- project_euler/problem_078/sol1.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index f061efcaf8c5..36ff920675ab 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -27,6 +27,7 @@ def solution() -> str: MODULUS = 10 ** 6 partitions = [1] result = "" + for i in itertools.count(len(partitions)): item = 0 @@ -46,5 +47,6 @@ def solution() -> str: if item == 0: result = str(i) + break return result From cea6f3b26bd52902fa59c238f30bd4106b713bf2 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Sun, 24 Oct 2021 15:07:55 +0530 Subject: [PATCH 16/23] Update doctest sol --- project_euler/problem_078/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 36ff920675ab..2889498eeb37 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -22,7 +22,7 @@ def solution() -> str: """ >>> solution() - 55374 + '55374' """ MODULUS = 10 ** 6 partitions = [1] From f540cc6077d87192aa7cc922ee99ad49fd509f5a Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 19:11:15 +0530 Subject: [PATCH 17/23] Update project_euler/problem_078/sol1.py Co-authored-by: John Law --- project_euler/problem_078/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 2889498eeb37..47014040377a 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -19,7 +19,7 @@ import itertools -def solution() -> str: +def solution(number: int = 1000000) -> str: """ >>> solution() '55374' From cc00359f1476d02fc37ec692ed5a4a43cde95593 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 22:19:38 +0530 Subject: [PATCH 18/23] Added doctest and changed return type --- project_euler/problem_078/sol1.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 47014040377a..33ad396d8a12 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -19,10 +19,10 @@ import itertools -def solution(number: int = 1000000) -> str: +def solution(number: int = 1000000) -> int: """ >>> solution() - '55374' + 55374 """ MODULUS = 10 ** 6 partitions = [1] @@ -46,7 +46,15 @@ def solution(number: int = 1000000) -> str: partitions.append(item) if item == 0: - result = str(i) + result = i break return result + + +if __name__ == "__main__": + + import doctest + + doctest.testmod() + print(f"{solution() = }") From 3ff95ac9dc44562f9d546eedb3adbf36702f9701 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 22:25:59 +0530 Subject: [PATCH 19/23] Add int() --- project_euler/problem_078/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 33ad396d8a12..d754ff779bee 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -46,7 +46,7 @@ def solution(number: int = 1000000) -> int: partitions.append(item) if item == 0: - result = i + result = int(i) break return result From 95ba5302ce5a7e1d14b96cf1521c317ff7af151d Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 22:32:35 +0530 Subject: [PATCH 20/23] Fix flake8 issues --- project_euler/problem_078/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index d754ff779bee..92b84f2cd683 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -49,7 +49,7 @@ def solution(number: int = 1000000) -> int: result = int(i) break - return result + return int(result) if __name__ == "__main__": From 4760d472eb9d759e67b21d1ae6bb426154550c16 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 26 Oct 2021 22:41:05 +0530 Subject: [PATCH 21/23] Use argument instead of fixed constant --- project_euler/problem_078/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 92b84f2cd683..495a326c7209 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -24,9 +24,9 @@ def solution(number: int = 1000000) -> int: >>> solution() 55374 """ - MODULUS = 10 ** 6 + partitions = [1] - result = "" + result = 0 for i in itertools.count(len(partitions)): @@ -41,7 +41,7 @@ def solution(number: int = 1000000) -> int: if index > i: break item += partitions[i - index] * sign - item %= MODULUS + item %= number partitions.append(item) From e4001b4bee1165682ffb70a7f2879277d0655822 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 27 Oct 2021 17:05:03 +0800 Subject: [PATCH 22/23] Update sol1.py --- project_euler/problem_078/sol1.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index 495a326c7209..a55aa4990e76 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -24,12 +24,10 @@ def solution(number: int = 1000000) -> int: >>> solution() 55374 """ - partitions = [1] result = 0 for i in itertools.count(len(partitions)): - item = 0 for j in itertools.count(1): sign = -1 if j % 2 == 0 else +1 @@ -43,18 +41,16 @@ def solution(number: int = 1000000) -> int: item += partitions[i - index] * sign item %= number - partitions.append(item) - if item == 0: - result = int(i) - break + return result + partitions.append(item) - return int(result) + return result if __name__ == "__main__": - import doctest doctest.testmod() + print(f"{solution() = }") From a227be9343d8fdb4cab17ba6e5aa830b172f8923 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 27 Oct 2021 17:11:33 +0800 Subject: [PATCH 23/23] fix sol1.py --- project_euler/problem_078/sol1.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_078/sol1.py b/project_euler/problem_078/sol1.py index a55aa4990e76..f92cf0f4020c 100644 --- a/project_euler/problem_078/sol1.py +++ b/project_euler/problem_078/sol1.py @@ -25,7 +25,6 @@ def solution(number: int = 1000000) -> int: 55374 """ partitions = [1] - result = 0 for i in itertools.count(len(partitions)): item = 0 @@ -42,10 +41,10 @@ def solution(number: int = 1000000) -> int: item %= number if item == 0: - return result + return i partitions.append(item) - return result + return 0 if __name__ == "__main__":