Created
August 5, 2016 16:32
-
-
Save BarnabasMarkus/6516cbaed9e91a9b71dcbaa7f22d67b3 to your computer and use it in GitHub Desktop.
sieve of eratosthenes // find primes algorithm
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
#!/usr/bin/env python3 | |
# S I E V E O F E R A T O S T H E N E S | |
# Project Sieve of Eratosthenes Prime Finder | |
# Author Barnabas Markus | |
# Email [email protected] | |
# Date 05.08.2016 | |
# Python 3.5.1 | |
# License MIT | |
from math import sqrt | |
def sieve(n=100): | |
lst = [x for x in range(0, n+1)] | |
lst[1] = 0 | |
limit = int(sqrt(n)) + 1 | |
for i in range(2, limit): | |
if lst[i] == 0: | |
continue | |
for j in range(i*2, len(lst), i): | |
if lst[j] != 0: | |
lst[j] = 0 | |
return [i for i in lst if i != 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment