Project Euler 10: (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 02:49:44 +0000 Indexed on 2010/12/06 16:59 UTC
Read the original article Hit count: 467

In my attempt to learn (Iron)Python out in the open, here’s my solution for Project Euler Problem 10

As always, any feedback is welcome.

# Euler 10
# http://projecteuler.net/index.php?section=problems&id=10
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
# Find the sum of all the primes below two million.
import time
start = time.time()

def primes_to_max(max):
    primes, number = [2], 3        

    while number < max:
        isPrime = True
        for prime in primes:
            if number % prime == 0:
                isPrime = False
                break
            if (prime * prime > number):
                break
        if isPrime:
            primes.append(number)   

        number += 2                 

    return primes

primes = primes_to_max(2000000)
print sum(primes)

print "Elapsed Time:", (time.time() - start) * 1000, "millisecs"
a=raw_input('Press return to continue')

© Johnny Coder or respective owner

Related posts about ironpython

Related posts about languages