Euler 0034
The Problem:
Find the sum of all numbers equal to the sum of their factorials.
Considerations and Approach:
We can find the upper limit by filling a number with 9s and checking that the sum of the digits is greater than the factorial of the number.
After we find the upper limit, we increment through it and check each ones factorial, then take the sum of the digits.
The Code:
import math
#find the upper limit
digits = 9
sum_digits = math.factorial(9)
while sum_digits > digits:
digits *= 10
digits += 9
sum_digits += math.factorial(9)
print(len(str(digits)), digits, sum_digits)
### part 2 since we now know the upper limit
upper_limit = 2540160
fact_nums = []
for i in range(3, upper_limit):
if i == sum([math.factorial(int(x)) for x in str(i)]):
fact_nums += [i]
print(fact_nums)
No comments to display
No comments to display