Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
- #Find the sum of all the primes below two million.
- #isPrime algorithm for MIT 6.00x course notes
- def isPrime(n):
- '''check if integer n is a prime'''
- # range starts with 2 and only needs to go up the square-root of n
- for x in range(2, int(n**0.5)+1):
- if n % x == 0:
- return False
- return True
- acc = 5 #sum of primes 2 and 3
- for i in range (5, 2000001,2): #loop over odd number
- if i%3 != 0 and isPrime(i): #check if number not divisible by 3 then check isPrime
- acc += i
- print acc
Advertisement
Add Comment
Please, Sign In to add comment