嵇爾的吐槽

#没事画轮子的嵇尔不定期的(W)碎(E)碎(B)念(B)和(L)吐(O)槽(G)

Problem 34 Digit factorials 2016-10-15 08:38:55

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.


各位数字的阶乘

145是个有趣的数,因为1! + 4! + 5! = 1 + 24 + 120 = 145。

找出所有各位数字的阶乘和等于其本身的数,并求它们的和。

注意:因为1! = 1和2! = 2不是和的形式,所以它们并不在讨论范围内。

def factorial(number):
    result = 1
    for i in range(1, number + 1):
        result *= i
    return result

def number_to_list(number):
    result = []
    while number:
        n = number % 10
        number = number / 10
        result.append(n)
    return result

def get_number_sum(number):
    number_list = number_to_list(number)
    result = 0
    for i in number_list:
        result += factorial(i)
    return result

for i in range(3, 9999999):
    if i == get_number_sum(i):
        print i

感想:

  1. [Finished in 81.8s] 超过了1分钟了,后面至少70秒都在浪费时间
  2. 中文翻译网站居然有人在题目下面贴答案,太不高兴了
评论共