Solution to problem 34

This commit is contained in:
David Doblas Jiménez 2021-09-01 10:52:36 +02:00
parent efe87996e5
commit 6ec7ed6537

41
src/Python/Problem034.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
"""
Created on 02 Apr 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for problem 34 of Project Euler
https://projecteuler.net/problem=34
"""
import math
from utils import timeit
@timeit("Problem 34")
def compute():
"""
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.
"""
ans = 0
for num in range(10,2540160):
sum_of_factorial = 0
for digit in str(num):
sum_of_factorial += math.factorial(int(digit))
if sum_of_factorial == num:
ans += sum_of_factorial
return ans
if __name__ == "__main__":
print(f"Result for Problem 34: {compute()}")