Created
January 10, 2014 02:19
-
-
Save kaipakartik/8345925 to your computer and use it in GitHub Desktop.
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#We take the opposite approach here and find all the composite numbers | |
# upto 2 million and then we sum what is not composite(prime). | |
def prime(n): | |
composite = range(n) | |
for a in xrange(2, n): | |
b = a | |
for b in xrange(a * a, n, a): | |
composite[b] = 0; | |
sum = 0 | |
for i in xrange(2, n): | |
if composite[i] != 0: | |
sum = sum + i | |
return sum | |
print prime(2000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment