forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp034.py
More file actions
34 lines (26 loc) · 1.04 KB
/
p034.py
File metadata and controls
34 lines (26 loc) · 1.04 KB
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
#
# Solution to Project Euler problem 34
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import math, sys
if sys.version_info.major == 2:
range = xrange
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())