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