码迷,mamicode.com
首页 > 编程语言 > 详细

Euler Project question 34 in python way

时间:2014-10-30 19:03:14      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   on   art   bs   ef   as   

# 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

标签:io   ar   for   sp   on   art   bs   ef   as   

原文地址:http://www.cnblogs.com/cyberpaz/p/4063216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!