Skip to content

Instantly share code, notes, and snippets.

@GEEGABYTE1
Created August 30, 2021 21:12
Show Gist options
  • Save GEEGABYTE1/3fa1de57b4439421537eb59b3736a4f3 to your computer and use it in GitHub Desktop.
Save GEEGABYTE1/3fa1de57b4439421537eb59b3736a4f3 to your computer and use it in GitHub Desktop.
Optimization of Sieve of Eratosthenes
import math
def sieve_of_eratosthenes (limit):
if (limit <= 1):
return []
output = [True] * (limit+1)
output[0] = False
output[1] = False
for i in range(2, math.floor(math.sqrt(limit))):
if (output[i] == True):
j = i ** 2
while j <= limit:
output[j] = False
j += i
output_with_indices = list(enumerate(output))
trues = [index for (index,value) in output_with_indices if value == True]
return trues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment