import timeit
def sieve_of_eratosthene(N):
# Lista de números primos:
numbers = []
# Cria-se uma lista referente a todos os inteiros entre 0 e N:
A = [True] * (N+1)
# Define os números 0 e 1 como não primos:
A[0] = A[1] = False
# Percorra a lista até encontrar o primeiro número primo:
for value, prime in enumerate(A):
# O número é primo?
if prime:
# Retorna o número primo:
numbers.append(value)
# Remova da lista todos os múltiplos do número enontrado:
for i in range(value**2, N+1, value):
A[i] = False
return numbers
def main():
primes = sieve_of_eratosthene(2750159)
print("Saida:", [primes[i] for i in [7, 1, 199999, 4]])
print("Tempo: {}s".format(timeit.timeit(main, number=1)))
aW1wb3J0IHRpbWVpdAogCmRlZiBzaWV2ZV9vZl9lcmF0b3N0aGVuZShOKToKICAgIAogICAgIyBMaXN0YSBkZSBuw7ptZXJvcyBwcmltb3M6CiAgICBudW1iZXJzID0gW10KIAogICAgIyBDcmlhLXNlIHVtYSBsaXN0YSByZWZlcmVudGUgYSB0b2RvcyBvcyBpbnRlaXJvcyBlbnRyZSAwIGUgTjoKICAgIEEgPSBbVHJ1ZV0gKiAoTisxKQogCiAgICAjIERlZmluZSBvcyBuw7ptZXJvcyAwIGUgMSBjb21vIG7Do28gcHJpbW9zOgogICAgQVswXSA9IEFbMV0gPSBGYWxzZQogCiAgICAjIFBlcmNvcnJhIGEgbGlzdGEgYXTDqSBlbmNvbnRyYXIgbyBwcmltZWlybyBuw7ptZXJvIHByaW1vOgogICAgZm9yIHZhbHVlLCBwcmltZSBpbiBlbnVtZXJhdGUoQSk6CiAKICAgICAgICAjIE8gbsO6bWVybyDDqSBwcmltbz8KICAgICAgICBpZiBwcmltZToKIAogICAgICAgICAgICAjIFJldG9ybmEgbyBuw7ptZXJvIHByaW1vOgogICAgICAgICAgICBudW1iZXJzLmFwcGVuZCh2YWx1ZSkKIAogICAgICAgICAgICAjIFJlbW92YSBkYSBsaXN0YSB0b2RvcyBvcyBtw7psdGlwbG9zIGRvIG7Dum1lcm8gZW5vbnRyYWRvOgogICAgICAgICAgICBm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