-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34.py
41 lines (29 loc) · 1 KB
/
34.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
f=[1]
for i in range(1,10):
f.append(f[-1]*i)
ans=0
for i in range(10,10**7):
s=0
for j in str(i):
s+=f[ord(j)-ord('0')]
if s==i:
print("sum",i)
ans+=i
print(ans)
import math
def compute():
# As stated in the problem, 1 = 1! and 2 = 2! are excluded.
# If a number has at least n >= 8 digits, then even if every digit is 9,
# n * 9! is still less than the number (which is at least 10^n).
ans = sum(i for i in range(3, 10000000) if i == factorial_digit_sum(i))
return str(ans)
def factorial_digit_sum(n):
result = 0
while n >= 10000:
result += FACTORIAL_DIGITS_SUM_WITH_LEADING_ZEROS[n % 10000]
n //= 10000
return result + FACTORIAL_DIGITS_SUM_WITHOUT_LEADING_ZEROS[n]
FACTORIAL_DIGITS_SUM_WITHOUT_LEADING_ZEROS = [sum(math.factorial(int(c)) for c in str(i)) for i in range(10000)]
FACTORIAL_DIGITS_SUM_WITH_LEADING_ZEROS = [sum(math.factorial(int(c)) for c in str(i).zfill(4)) for i in range(10000)]
if __name__ == "__main__":
print(compute())