Friday, September 13, 2013

Problem 10 - Summation of Primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
_________________________________________________________________________________

import math

def prime(n):
 i=2
 k = 1+math.floor(math.sqrt(n))
 while i<k and n%i != 0:
    i = i+1
 if i == k:
  return True
 else:
  return False

i=2
j=0
while i<2000000:
 if prime(i)==True:
  j=j+i
 i=i+1

print j


No comments:

Post a Comment