Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

sieve of eratosthenes python

# sieve of eratosthenes 
def SieveOfEratosthenes(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):
        if (prime[p] == True):
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    prime[0] = False
    prime[1] = False
    r = []
    for p in range(n + 1):
        if prime[p]:
            r.append(p)
    return r

n = int(input("Get primes in range of?- "))
listOfPrime = SieveOfEratosthenes(n)
count = 1
# print primes 1-100 then 101-200 etc. in different line
for i in listOfPrime:
    if i > 100 * count:
        count += 1
        print()
    print(i, end = " ")
 
PREVIOUS NEXT
Tagged: #sieve #eratosthenes #python
ADD COMMENT
Topic
Name
8+4 =