zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    连通块问题
    线性数据结构
    NOIP2018总结
    原码反码补码详解
    一些常用的算法技巧总结
    骗分导论
    模板
    模板
    AcWing
    AcWing
  • 原文地址:https://www.cnblogs.com/cyberpaz/p/4063216.html
Copyright © 2011-2022 走看看