mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Rename Project Euler directories and other dependent changes (#3300)
* Rename all Project Euler directories: Reason: The change was done to maintain consistency throughout the directory and to keep all directories in sorted order. Due to the above change, some config files had to be modified: 'problem_22` -> `problem_022` * Update scripts to pad zeroes in PE directories
This commit is contained in:
1
project_euler/problem_034/__init__.py
Normal file
1
project_euler/problem_034/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
#
|
||||
36
project_euler/problem_034/sol1.py
Normal file
36
project_euler/problem_034/sol1.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
Problem 34: https://projecteuler.net/problem=34
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
from math import factorial
|
||||
|
||||
|
||||
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(char)) for char in str(n))
|
||||
|
||||
|
||||
def solution() -> int:
|
||||
"""
|
||||
Returns the sum of all numbers whose
|
||||
sum of the factorials of all digits
|
||||
add up to the number itself.
|
||||
>>> solution()
|
||||
40730
|
||||
"""
|
||||
limit = 7 * factorial(9) + 1
|
||||
return sum(i for i in range(3, limit) if sum_of_digit_factorial(i) == i)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"{solution()} = ")
|
||||
Reference in New Issue
Block a user