首页 > 代码库 > Euler Project question 34 in python way

Euler Project question 34 in python way

# 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

# Find the sum of all numbers which are equal to the sum of the factorial of their digits.

# Note: as 1! = 1 and 2! = 2 are not sums they are not included.
import time
start = time.time()
L = []
def integers():
    i = 3
    while True:
        yield i
        i += 1
def factorial(n):
    c = 1
    for i in range(2, n+1):
        c *= i
    return c
integer = integers()
for i in integer:
    if i < len(str(i)) * factorial(9):
        if i == sum([factorial(int(k)) for k in str(i)]):
            L.append(i)
        else:
            continue
    else:
        break
print "%s found in %s seconds" % (sum(L), time.time() - start)

Euler Project question 34 in python way