Project Euler 1: (Iron)Python
- by Ben Griswold
In my attempt to learn (Iron)Python out in the open, here’s my solution for Project Euler Problem 1.
As always, any feedback is welcome.
# Euler 1
# http://projecteuler.net/index.php?section=problems&id=1
# If we list all the 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
start = time.time()
print sum([x for x in range(1000) if x % 3== 0 or x % 5== 0])
print "Elapsed Time:", (time.time() - start) * 1000, "millisecs"
a=raw_input('Press return to continue')
# Also cool
def constraint(x):
return x % 3 == 0 or x % 5 == 0
print sum(filter(constraint, range(1000)))