Skip to content

Instantly share code, notes, and snippets.

@pjwerneck
Last active February 22, 2016 19:56
Show Gist options
  • Select an option

  • Save pjwerneck/044577f0347045a8d4e3 to your computer and use it in GitHub Desktop.

Select an option

Save pjwerneck/044577f0347045a8d4e3 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Python
import time
def iprimes2(limit):
yield 2
if limit < 3:
return
lmtbf = (limit - 3) // 2
buf = [True] * (lmtbf + 1)
for i in xrange((int(limit ** 0.5) - 3) // 2 + 1):
if buf[i]:
p = i + i + 3
s = p * (i + 1) + i
buf[s::p] = [False] * ((lmtbf - s) // p + 1)
for i in xrange(lmtbf + 1):
if buf[i]:
yield (i + i + 3)
limite = int(input("Digite limite: "))
t = time.time()
with open('primos', 'w') as f:
f.write(str(list(iprimes2(100000100))))
print("\n")
print("Tempo executado: ", time.time() - t)
print("\n")
print("Foi gerado lista em arquivo de texto no local do programa")
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment