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

Euler Project question 1 in python way

时间:2014-10-15 23:39:21      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   2014   art   on   amp   ad   

# 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

 

Euler Project question 1 in python way

标签:io   ar   for   sp   2014   art   on   amp   ad   

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

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