Skip to content

Instantly share code, notes, and snippets.

@velociraptors
Created January 26, 2011 16:02
Show Gist options
  • Save velociraptors/796909 to your computer and use it in GitHub Desktop.
Save velociraptors/796909 to your computer and use it in GitHub Desktop.
LIMIT = 20001
NATURALS = range(3,LIMIT, 2)
PRIMES = [2]
current = 0
max = LIMIT
while len(NATURALS) > 0:
current = NATURALS.pop(0)
PRIMES.append(current)
i = current
try:
max = NATURALS[-1]
except IndexError:
break
while i <= max:
i = i + current
try:
NATURALS.remove(i)
except ValueError:
pass
print('primes: {0}'.format(PRIMES))
print('sum: {0}'.format(sum(PRIMES)))
@SavinaRoja
Copy link

LIMIT = 2000001
NATURALS = range(3,LIMIT, 2)
PRIMES = [2]
current = 0

while len(NATURALS) > 0:
current = NATURALS.pop(0)
PRIMES.append(current)
try:
n = NATURALS[-1] / current
except IndexError:
pass
i = current
while n > 1:
i = n * current
try:
NATURALS.remove(i)
except ValueError:
pass
n -= 1

print('sum: {0}'.format(sum(PRIMES)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment