-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheuler034.py
54 lines (42 loc) · 1.01 KB
/
euler034.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
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
19 is a curious number, as 1! + 9! = 1 + 362880 = 362881 which is divisible by 19.
Find the sum of all numbers below N which divide the sum of the factorial of their digits.
Note: as 1!, 2!, ..., 9! are not sums they are not included.
"""
import math
import random
fdict = {}
def factorial(n):
if n not in fdict:
if n <= 1:
fdict[n] = 1
else:
fdict[n] = n * factorial(n - 1)
return fdict[n]
def curiousQ(n):
cumsum = 0
n_old = n
while n / 10 > 0:
cumsum += factorial(n % 10)
n /= 10
cumsum += factorial(n % 10)
return cumsum % n_old == 0
def main():
n = long(input().strip())
# n = random.randint()
l = []
# n = 10**5
cumsum = 0
for x in range(10, n):
if curiousQ(x):
cumsum += x
l.append(cumsum)
print
cumsum
return 0
if __name__ == '__main__':
# print "This program is being run by itself"
main()
else:
print
'I am being imported from another module'