06.03.08

Project Euler: Problem 10

Posted in python tagged , , , at 4:26 pm by redochre

Problem 10:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

A solution:

import math

def isPrime(num):
    if type(num) != int: return False
    if num == 2: return True
    if num < 2 or num % 2 == 0: return False
    return not any(num % i == 0 for i in range(3, int(math.sqrt(num))+1, 2))

sum = 0
for i in range(2,2000000):
    if isPrime(i): sum += i

print sum

Straightforward, yes?