Skip to content

Instantly share code, notes, and snippets.

@kaipakartik
Created January 10, 2014 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaipakartik/8345925 to your computer and use it in GitHub Desktop.
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.
#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