Last active
October 30, 2019 07:28
-
-
Save Bablzz/3a8655cce3af97edad1018da9a092a4b to your computer and use it in GitHub Desktop.
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
# O(n log(log n)) | |
arr = list(range(0, 300)) | |
def sieve(ar): | |
for i in ar: | |
if i > 1: | |
step = arr[i] | |
while (arr[i] + step) < len(arr): | |
step = arr[i] + step | |
arr[step] = 0 | |
return ar | |
arr_with_zero = [i for i in sieve(arr) if i != 0] | |
print(arr_with_zero) | |
#1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163 | |
#, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment