[ create a new paste ] login | about

Link: http://codepad.org/f2nHK44x    [ raw code | output | fork ]

programmingpraxis - Python, pasted on Jan 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# sum of primes less than n
# sieve of eratosthenes

from time import time

def sumPrimes(n):
    i, p, s, m = 0, 3, 2, n // 2
    sieve = [True] * m
    while p <= n:
        if sieve[i]:
            s += p
            for j in range((p*p-3)/2, m, p):
                sieve[j] = False
        i, p = i+1, p+2
    return s

start = time()
print sumPrimes(2000000)
print int(round(1000 * (time() - start))), "milliseconds"


Output:
1
2
142913828922
1333 milliseconds


Create a new paste based on this one


Comments: