Created
August 30, 2021 21:12
-
-
Save GEEGABYTE1/3fa1de57b4439421537eb59b3736a4f3 to your computer and use it in GitHub Desktop.
Optimization of Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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