Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Summation of primes : Python Code Problem 10 Euler Project

Saturday, August 30, 2014

Summation of primes : Python Code Problem 10 Euler Project

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