Sunday, August 3, 2014

Project Euler Problem 10 Solution

Summation of primes

Problem 10

Published on 08 February 2002 at 06:00 pm [Server Time]
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million


Python Code:
-------------------------------------

# function to check if number is prime

def is_prime(num):
# prime numbers are greater than 1
   # check for factors
   for i in range(2,num/2 + 1):
       if (num % i) == 0:
           #print(num,"is not a prime number")
           return False
   else:
       #print(num,"is a prime number")
        return True

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

print sum









No comments:

Post a Comment