From b3ae39249dc0f6f2c02b3a535aa4c95a32c361a7 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 13 Aug 2020 20:22:47 +0530 Subject: [PATCH] Created problem_34 in project_euler (#2305) * Create __init__.py * Add files via upload * Rename solution.py.py to solution.py * Delete __init__.py * Update and rename solution.py to sol1.py * Update sol1.py * Create __init__.py * Update __init__.py * Delete __init__.py * Add files via upload * Update __init__.py * Add #\n * Update sol1.py Incorporates the proposed changes * Update sol1.py * Update sol1.py * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss * Update sol1.py * Update sol1.py * Update sol1.py * Use int(n) instead of floor(n) Co-authored-by: Christian Clauss --- project_euler/problem_34/__init__.py | 1 + project_euler/problem_34/sol1.py | 67 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 project_euler/problem_34/__init__.py create mode 100644 project_euler/problem_34/sol1.py diff --git a/project_euler/problem_34/__init__.py b/project_euler/problem_34/__init__.py new file mode 100644 index 000000000..792d60054 --- /dev/null +++ b/project_euler/problem_34/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py new file mode 100644 index 000000000..126aee9d2 --- /dev/null +++ b/project_euler/problem_34/sol1.py @@ -0,0 +1,67 @@ +""" +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) + 1 + >>> factorial(0) + 1 + >>> 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 + """ + + if not n >= 0: + raise ValueError("n must be >= 0") + 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") + 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 + """ + return sum(factorial(int(digit)) for digit in str(n)) + + +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 + """ + return sum( + num + for num in range(3, 7 * factorial(9) + 1) + if sum_of_digit_factorial(num) == num + ) + + +if __name__ == "__main__": + print(compute())