-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler34.py
More file actions
32 lines (23 loc) · 799 Bytes
/
Copy patheuler34.py
File metadata and controls
32 lines (23 loc) · 799 Bytes
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
# a curious number is the sum of the factorial of its digits
# eg 145 = 1!+4!+5!
import time
factorials = [1]
factorials = [factorials[-1] for n in range(0, 10) if not factorials.append(max(n,1) * factorials[-1])]
curiousSum = 0
def numberIsCurious(number):
n = number
facSum = 0
while n > 9:
facSum += factorials[n%10]
n //= 10
return number == facSum + factorials[n]
if __name__ == '__main__':
start = time.time()
for i in range(3, 2177281): # big upper bound based on 6 * 9! + 1 as an easy limit
if numberIsCurious(i):
curiousSum += i
print(curiousSum)
end = time.time()
print(f"{end-start}")
# maybe speed gains to be had from avoiding repeated work. dynamic programming
# tried this and slowed things down a lot..