* Computer Science/Project Euler

project euler 34. problem

soicem 2016. 8. 29. 16:42

숫자 145에는 신기한 성질이 있습니다. 각 자릿수의 팩토리얼(계승)을 더하면  1! + 4! + 5! = 1 + 24 + 120 = 145 처럼 자기 자신이 됩니다.

이렇게 각 자릿수의 팩토리얼을 더하면 자기 자신이 되는 모든 수의 합을 구하세요.

단, 1! = 1 과 2! = 2 의 경우는 덧셈이 아니므로 제외합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def fact(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    return result
 
result = 0
 
for i in range(33000000):
    tmp = str(i)
    Sum = 0
    for j in range(0len(tmp)):
        Sum += fact(int(tmp[j]))
    if Sum == i:
        print i
        result += i
 
print result 
 
cs



C:\Users\sec\Desktop\연습>python test.py

145

40585

40730