Skip to content

Sieve of Eratosthenes implemented in python #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions allalgorithms/numeric/sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Numeric Algorithms
# Contributed by: JordaoA

#Sieve of Eratosthenes implemented in python

def cieve(rangeOfPrimes):
try:
rangeOfPrimes = int(rangeOfPrimes)
primes = []

if rangeOfPrimes < 2:
return("invalid Range :(")

elif rangeOfPrimes == 2:
primes.append(2)
return(primes)

else:
cieve = [True] * rangeOfPrimes
cieve[0] = False
cieve[1] = False

for i in range(2,rangeOfPrimes):
if cieve[i]:
for j in range(i*2,rangeOfPrimes,i):
cieve[j] = False

for i in range(rangeOfPrimes):
if cieve[i]:
primes.append(i)

return(primes)

except:
return("invalid Type :(")