• Euler Project question 1 in python way


    # if we list all natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
    import time
    t0 = time.time()
    sum = 0
    for i in range(1, 1000):
      if (i % 3 == 0) or (i % 5 == 0):
        sum = sum + i
    print(sum)
    t1 = time.time()
    print "Process usage", t1 - t0

    # result
    # 233168
    # Process usage 0.00100016593933


    adding at 11.10.2014

    # using operator, list, for loop&conditions to simplify creating a list, best version
    import time
    start = time.time()
    print sum([i for i in range(1, 1000) if i % 3 == 0 or i % 5 == 0])
    print "Process usage:", time.time() - start

    # result
    # 233168
    # Process usage: 0.000999927520752

    # using xrang()
    # import time
    # start = time.time()
    # print sum([i for i in xrange(1, 1000) if i % 3 == 0 or i % 5 == 0])
    # print "Process usage:", time.time() - start

    # result
    # 233168
    # Process usage: 0.00200009346008

  • 相关阅读:
    jquery animate() stop() finish() 方法使用
    ant 相关命令
    ant 安装 网址
    邮件
    webdriver until
    python HTML报告
    登录
    yun
    centos7搭建smb服务
    爬取图片
  • 原文地址:https://www.cnblogs.com/cyberpaz/p/4027403.html
Copyright © 2020-2023  润新知