princeofsudan

ProjectEuler#10 Summation of primes

Mar 2nd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. #The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
  2.  
  3. #Find the sum of all the primes below two million.
  4.  
  5. #isPrime algorithm for MIT 6.00x course notes
  6.  
  7. def isPrime(n):
  8.     '''check if integer n is a prime'''
  9.     # range starts with 2 and only needs to go up the square-root of n
  10.     for x in range(2, int(n**0.5)+1):
  11.         if n % x == 0:
  12.             return False
  13.     return True
  14.  
  15. acc = 5 #sum of primes 2 and 3
  16. for i in range (5, 2000001,2): #loop over odd number
  17.  
  18.     if i%3 != 0 and isPrime(i): #check if number not divisible by 3 then check isPrime
  19.         acc += i
  20.  
  21. print acc
Advertisement
Add Comment
Please, Sign In to add comment