Project Euler 1: (Iron)Python

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Mon, 13 Sep 2010 00:05:11 +0000 Indexed on 2010/12/06 16:59 UTC
Read the original article Hit count: 322

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)))

© Johnny Coder or respective owner

Related posts about ironpython

Related posts about languages