Last active
April 27, 2022 13:18
-
-
Save alexmaryin/8e43d996e316687f75d619094517d63f to your computer and use it in GitHub Desktop.
Python_sieve
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 time | |
def measured_time(func): | |
def wrapped(*args): | |
start = time.perf_counter_ns() | |
result = func(*args) | |
elapsed = int((time.perf_counter_ns() - start) / 1000000) | |
print(f'Elapsed time is {elapsed} ms') | |
return result | |
return wrapped | |
@measured_time | |
def sieve_Eratosphen(n) -> list[int]: | |
assert n > 2 | |
nxt = 2 | |
numbers = [True for _ in range(n)] | |
while nxt * nxt <= n: | |
if numbers[nxt]: | |
for index in range(n - nxt): | |
step = nxt * nxt + index * nxt | |
if step < n: | |
numbers[step] = False | |
else: | |
break | |
nxt += 1 | |
return [idx for idx, is_prime in enumerate(numbers) if idx >= 2 and is_prime] | |
if __name__ == '__main__': | |
print(f'Last prime is {sieve_Eratosphen(100000000)[-1]}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment