In: Computer Science
PYTHON: Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to n. First insert all numbers from 1 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, . . . . Erase all multiples of 3, that is, 6, 9, 12, 15, ... . Go up to √ n. The remaining numbers are all primes.
Please solve in python!
"""
sieve_of_Eratosthenes prime numbers method : first
removing multiples of 2's except 2. From that removing multiples of
3's except 3.
From that removing multiples of 5's except 5.From that
removing multiples of 7's except 7.
"""
def sieve_of_Eratosthenes(n):
s=[]
for i in range(0,n):
s.append(i+1)
#removing multiples of 2's except 2
for ele in s:
if(ele==1):
s.remove(ele)
if(ele%2==0):
if(ele==2):
continue
s.remove(ele)
#removing multiples of 3's except 3.
for ele in s:
if(ele%3==0):
if(ele==3):
continue
s.remove(ele)
#removing multiples of 5's except 5.
for ele in s:
if(ele%5==0):
if(ele==5):
continue
s.remove(ele)
#removing multiples of 7's except 7.
for ele in s:
if(ele%7==0):
if(ele==7):
continue
s.remove(ele)
print("prime nubmers up to ",n,set(s))
#main program
n=int(input("Enter number"))
sieve_of_Eratosthenes(n)