Summation of primes : 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.
Problem Source: Euler Project
Python Code
import math
def sumPrime():
primeSum = 17
def checkPrime(numb):
prime = True
for i in range(int(math.sqrt(numb))):
i = i+2
if numb%i == 0:
prime = False
break
if numb == 2:
return True
else:
return prime
for i in range(2000000-1):
i = i+2
if i%2 == 0 or i%3 == 0 or i%5 ==0 or i%7 == 0:
continue
if checkPrime(i) == True:
primeSum = primeSum+i
print "Sum of all the primes below two million:",primeSum
sumPrime()
No comments:
Post a Comment