码迷,mamicode.com
首页 > 其他好文 > 详细

Problem 34

时间:2019-06-16 13:21:02      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:dig   ==   sum   阶乘   包括   等于   find   code   multi   

Problem 34

https://projecteuler.net/problem=34

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

145是一个神奇的数字,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.

注意:不包括1和2。

def factorial(num):
    f = 1
    for i in range(1, num+1):
        f *= i
    return f

tot = 0
nums = []
for i in range(3, 99999):
    print(i)
    digits = list(str(i))
    if i < factorial(int(max(digits))):
        continue
    multi = 0
    for digit in digits:
        multi += factorial(int(digit))
    if multi == i:
        nums.append(i)
        tot += i

print(nums)
print(tot)

 

Problem 34

标签:dig   ==   sum   阶乘   包括   等于   find   code   multi   

原文地址:https://www.cnblogs.com/noonjuan/p/11031246.html

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