From dcdaa1ecdccfe55411ea1d51515942c2bd637db0 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:22:39 +0530 Subject: [PATCH 01/23] Create __init__.py --- project_euler/problem_34/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_34/__init__.py diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/project_euler/problem_34/__init__.py @@ -0,0 +1 @@ + From 16f0943c4aa6db08bdeb2b8f664ea5dadb9b5a34 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:24:26 +0530 Subject: [PATCH 02/23] Add files via upload --- project_euler/problem_34/solution.py.py | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 project_euler/problem_34/solution.py.py diff --git a/project_euler/problem_34/solution.py.py b/project_euler/problem_34/solution.py.py new file mode 100644 index 000000000000..341164deafb9 --- /dev/null +++ b/project_euler/problem_34/solution.py.py @@ -0,0 +1,58 @@ +""" +145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. + +Find the sum of all numbers which are equal to the sum of the factorial of their digits. + +Note: As 1! = 1 and 2! = 2 are not sums they are not included. + +""" + + +def factorial(n: int) -> int: + """ + Returns the factorial of n + >>> factorial(5) + 120 + >>> factorial(1) + 1 + >>> factorial(0) + 1 + """ + result = 1 + factor = 2 + while factor <= n: + result *= factor + factor += 1 + return result + + +def sum_of_digit_factorial(n: int) -> int: + """ + Returns the sum of the digits in n + >>> sum_of_digit_factorial(15) + 121 + >>> sum_of_digit_factorial(0) + 1 + """ + digits = list(map(int, str(n))) + summ = sum(factorial(digit) for digit in digits) + return summ + + +def compute() -> int: + """ + Returns the sum of all numbers whose + sum of the factorials of all digits + add up to the number itself. + >>> compute() + 40730 + """ + summ = 0 + for num in range(3, 7 * factorial(9) + 1): + if sum_of_digit_factorial(num) == num: + summ += num + return summ + + +if __name__ == "__main__": + print(compute()) \ No newline at end of file From f7d92f87dd9ad57fa6abcb858558f8ebfd803490 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:25:01 +0530 Subject: [PATCH 03/23] Rename solution.py.py to solution.py --- project_euler/problem_34/{solution.py.py => solution.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename project_euler/problem_34/{solution.py.py => solution.py} (93%) diff --git a/project_euler/problem_34/solution.py.py b/project_euler/problem_34/solution.py similarity index 93% rename from project_euler/problem_34/solution.py.py rename to project_euler/problem_34/solution.py index 341164deafb9..9e60b6608183 100644 --- a/project_euler/problem_34/solution.py.py +++ b/project_euler/problem_34/solution.py @@ -55,4 +55,4 @@ def compute() -> int: if __name__ == "__main__": - print(compute()) \ No newline at end of file + print(compute()) From a915dea51567ab6bab392f02c4dc75423d898934 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:26:42 +0530 Subject: [PATCH 04/23] Delete __init__.py --- project_euler/problem_34/__init__.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 project_euler/problem_34/__init__.py diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py deleted file mode 100644 index 8b137891791f..000000000000 --- a/project_euler/problem_34/__init__.py +++ /dev/null @@ -1 +0,0 @@ - From c47288ccc328dcb44e008de204fb5556a089dada Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:27:19 +0530 Subject: [PATCH 05/23] Update and rename solution.py to sol1.py --- project_euler/problem_34/{solution.py => sol1.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename project_euler/problem_34/{solution.py => sol1.py} (100%) diff --git a/project_euler/problem_34/solution.py b/project_euler/problem_34/sol1.py similarity index 100% rename from project_euler/problem_34/solution.py rename to project_euler/problem_34/sol1.py From 632b8385f623b6a0a3f5157bb0a05c7908f288fa Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:40:29 +0530 Subject: [PATCH 06/23] Update sol1.py --- project_euler/problem_34/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 9e60b6608183..f4fab22d7fc1 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -27,7 +27,7 @@ def factorial(n: int) -> int: def sum_of_digit_factorial(n: int) -> int: - """ + """ Returns the sum of the digits in n >>> sum_of_digit_factorial(15) 121 @@ -41,7 +41,7 @@ def sum_of_digit_factorial(n: int) -> int: def compute() -> int: """ - Returns the sum of all numbers whose + Returns the sum of all numbers whose sum of the factorials of all digits add up to the number itself. >>> compute() From f801415313ab56c858f6666f7c7c9a3ab8c76d4a Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 17:57:40 +0530 Subject: [PATCH 07/23] Create __init__.py --- project_euler/problem_34/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_34/__init__.py diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/project_euler/problem_34/__init__.py @@ -0,0 +1 @@ + From 39bdd9515e1d862a75b5490d8622ebd67aaf5b72 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 18:02:51 +0530 Subject: [PATCH 08/23] Update __init__.py From 2cf433f2aab61fd8670d024b815aced141fde711 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 18:06:36 +0530 Subject: [PATCH 09/23] Delete __init__.py --- project_euler/problem_34/__init__.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 project_euler/problem_34/__init__.py diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py deleted file mode 100644 index 8b137891791f..000000000000 --- a/project_euler/problem_34/__init__.py +++ /dev/null @@ -1 +0,0 @@ - From 0e7d56b65410d23b4f949594d9dc8eb9bd6c7177 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 18:12:54 +0530 Subject: [PATCH 10/23] Add files via upload --- project_euler/problem_34/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_34/__init__.py diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py new file mode 100644 index 000000000000..0519ecba6ea9 --- /dev/null +++ b/project_euler/problem_34/__init__.py @@ -0,0 +1 @@ + \ No newline at end of file From b5a56bb381886212476deaa3fafebfbe4b1913b6 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 18:15:53 +0530 Subject: [PATCH 11/23] Update __init__.py --- project_euler/problem_34/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py index 0519ecba6ea9..8b137891791f 100644 --- a/project_euler/problem_34/__init__.py +++ b/project_euler/problem_34/__init__.py @@ -1 +1 @@ - \ No newline at end of file + From fd043c0bae83da65bbb7047d472c812b578edeae Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 13 Aug 2020 15:02:48 +0200 Subject: [PATCH 12/23] Add #\n --- project_euler/problem_34/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py index 8b137891791f..792d6005489e 100644 --- a/project_euler/problem_34/__init__.py +++ b/project_euler/problem_34/__init__.py @@ -1 +1 @@ - +# From e551e5a1a4b1e173e0d336adad026059576dab9f Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:03:24 +0530 Subject: [PATCH 13/23] Update sol1.py Incorporates the proposed changes --- project_euler/problem_34/sol1.py | 34 ++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index f4fab22d7fc1..a613b0cb4bed 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -9,15 +9,33 @@ def factorial(n: int) -> int: - """ - Returns the factorial of n + """Return the factorial of n. + >>> factorial(5) 120 >>> factorial(1) 1 >>> factorial(0) - 1 + 0 + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: n must be >= 0 + + >>> factorial(1.1) + Traceback (most recent call last): + ... + ValueError: n must be exact integer """ + + import math + + if not n >= 0: + raise ValueError("n must be >= 0") + if math.floor(n) != n: + raise ValueError("n must be exact integer") + if n + 1 == n: # catch a value like 1e300 + raise OverflowError("n too large") result = 1 factor = 2 while factor <= n: @@ -27,7 +45,7 @@ def factorial(n: int) -> int: def sum_of_digit_factorial(n: int) -> int: - """ + """ Returns the sum of the digits in n >>> sum_of_digit_factorial(15) 121 @@ -41,17 +59,17 @@ def sum_of_digit_factorial(n: int) -> int: def compute() -> int: """ - Returns the sum of all numbers whose + Returns the sum of all numbers whose sum of the factorials of all digits add up to the number itself. >>> compute() 40730 """ - summ = 0 + the_list = [] for num in range(3, 7 * factorial(9) + 1): if sum_of_digit_factorial(num) == num: - summ += num - return summ + the_list.append(num) + return sum(the_list) if __name__ == "__main__": From 5e92188c96bff8c8ce2cf433c8278bb6e1114bc7 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:11:05 +0530 Subject: [PATCH 14/23] Update sol1.py --- project_euler/problem_34/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index a613b0cb4bed..c60daaab57a3 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -21,7 +21,7 @@ def factorial(n: int) -> int: Traceback (most recent call last): ... ValueError: n must be >= 0 - + >>> factorial(1.1) Traceback (most recent call last): ... @@ -45,7 +45,7 @@ def factorial(n: int) -> int: def sum_of_digit_factorial(n: int) -> int: - """ + """ Returns the sum of the digits in n >>> sum_of_digit_factorial(15) 121 @@ -59,7 +59,7 @@ def sum_of_digit_factorial(n: int) -> int: def compute() -> int: """ - Returns the sum of all numbers whose + Returns the sum of all numbers whose sum of the factorials of all digits add up to the number itself. >>> compute() From 0cf34594527af454497a9622495cc90cf0048c8d Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:26:06 +0530 Subject: [PATCH 15/23] Update sol1.py --- project_euler/problem_34/sol1.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index c60daaab57a3..fd8a30b2de9a 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -65,10 +65,11 @@ def compute() -> int: >>> compute() 40730 """ - the_list = [] - for num in range(3, 7 * factorial(9) + 1): - if sum_of_digit_factorial(num) == num: - the_list.append(num) + the_list = [ + num + for num in range(3, 7 * factorial(9) + 1) + if sum_of_digit_factorial(num) == num + ] return sum(the_list) From 07e43dfe4b10e53e1b31916a256e18944c4929cb Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:32:44 +0530 Subject: [PATCH 16/23] Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_34/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index fd8a30b2de9a..08a84c38504e 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -65,7 +65,7 @@ def compute() -> int: >>> compute() 40730 """ - the_list = [ + return sum( num for num in range(3, 7 * factorial(9) + 1) if sum_of_digit_factorial(num) == num From 5d76c7ee5b74f5610493665d9637fe0088bcb45c Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:33:03 +0530 Subject: [PATCH 17/23] Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_34/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 08a84c38504e..655c09f1a5fa 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -54,7 +54,7 @@ def sum_of_digit_factorial(n: int) -> int: """ digits = list(map(int, str(n))) summ = sum(factorial(digit) for digit in digits) - return summ + return sum(factorial(int(digit)) for digit in str(n)) def compute() -> int: From b5e771ff7971470c768ffd8f5d6ef6a577df527d Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:41:50 +0530 Subject: [PATCH 18/23] Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_34/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 655c09f1a5fa..a8514dd474c1 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -69,7 +69,7 @@ def compute() -> int: num for num in range(3, 7 * factorial(9) + 1) if sum_of_digit_factorial(num) == num - ] + ) return sum(the_list) From 8f0b7c15e89caf5cb0b543b70a51c51ea86e33bf Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:42:27 +0530 Subject: [PATCH 19/23] Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_34/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index a8514dd474c1..94b41e37fe17 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -70,7 +70,6 @@ def compute() -> int: for num in range(3, 7 * factorial(9) + 1) if sum_of_digit_factorial(num) == num ) - return sum(the_list) if __name__ == "__main__": From bd0849a551e0560fd91a5a3b287896ef2a5bd534 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:44:06 +0530 Subject: [PATCH 20/23] Update sol1.py --- project_euler/problem_34/sol1.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 94b41e37fe17..c11893568dbb 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -52,8 +52,6 @@ def sum_of_digit_factorial(n: int) -> int: >>> sum_of_digit_factorial(0) 1 """ - digits = list(map(int, str(n))) - summ = sum(factorial(digit) for digit in digits) return sum(factorial(int(digit)) for digit in str(n)) From 53cf0a53f8c0402d7fea70e54104102e4dcfbf44 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 19:56:06 +0530 Subject: [PATCH 21/23] Update sol1.py --- project_euler/problem_34/sol1.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index c11893568dbb..6e4aa44a2b71 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -1,16 +1,12 @@ """ 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - Note: As 1! = 1 and 2! = 2 are not sums they are not included. - """ def factorial(n: int) -> int: """Return the factorial of n. - >>> factorial(5) 120 >>> factorial(1) @@ -21,7 +17,6 @@ def factorial(n: int) -> int: Traceback (most recent call last): ... ValueError: n must be >= 0 - >>> factorial(1.1) Traceback (most recent call last): ... From b038727aca46a90a7e02da898d492ee7ee353724 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 20:08:29 +0530 Subject: [PATCH 22/23] Update sol1.py --- project_euler/problem_34/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 6e4aa44a2b71..7bfe93810069 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -12,7 +12,7 @@ def factorial(n: int) -> int: >>> factorial(1) 1 >>> factorial(0) - 0 + 1 >>> factorial(-1) Traceback (most recent call last): ... From 4c38019b7251d07db1b8b22913f373e59e207e58 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 13 Aug 2020 16:51:52 +0200 Subject: [PATCH 23/23] Use int(n) instead of floor(n) --- project_euler/problem_34/sol1.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 7bfe93810069..126aee9d2023 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -23,11 +23,9 @@ def factorial(n: int) -> int: ValueError: n must be exact integer """ - import math - if not n >= 0: raise ValueError("n must be >= 0") - if math.floor(n) != n: + if int(n) != n: raise ValueError("n must be exact integer") if n + 1 == n: # catch a value like 1e300 raise OverflowError("n too large")