# Sieve of Eratosthenes In Python

Hey Learner, Here in this article, we will learn the algorithm of Sieve of Eratosthenes In Python. So if you want to learn this algorithm these types of algorithms, then follow our site for easy to learn. So let’s get started.

Sieve of Eratosthenes is the algorithm, which is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so in a very fast way and easily with less space and time complexity.

### What is the Complexity of Sieve of Eratosthenes.

To find the prime number between 1 to n in O(N) time by using the Sieve Of Eratosthenes algorithm.

## Sieve of Eratosthenes In Python Algorithms for Sieve of Eratosthenes is used for prime number with less time complexity and fast in the compilation.

### Algorithms for Sieve of Eratosthenes

Step 1. Create array integers from 2 to n: (2, 3, 4, …, n).
Step 2. Initially, let p equal 2, the first prime number.
Step 3. Starting a loop from (p=2;p*p <=n;p++ )
Step 4. if ( prime [p] == true)
Step 5. For (i=p*p;i<=n ;i=i+p) prime[p]=false;
Step 6. and repeat from step 3.

## Program For Sieve of Eratosthenes in Python

``````def sieve(n):
arr = [1 for i in range(n+1) ]
i=2
while(i*i<=n):
if (arr[i] == 1):
for j in range(i*i, n+1, i):
arr[j]=0
i +=1
#print all the number which is prime
for i in range(2,n+1):
if(arr[i]==1):
print(i, end=' ')

#main
if __name__ =='__main__':
n= int(input())
sieve(n)``````
```Input : 50

Output : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47```