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 In Python

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

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) ]
    if (arr[i] == 1):
      for j in range(i*i, n+1, i):
    i +=1
  #print all the number which is prime
  for i in range(2,n+1):
      print(i, end=' ') 

if __name__ =='__main__':
  n= int(input())
Input : 50

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

So if you want to learn more python algorithms then follow this page Python.

Sieve of Eratosthenes Video Tutorial

Learn More Python Tutorials

If you have any queries regarding Sieve of Eratosthenes In Python, then ask in the comment section, I am happy to answer your question. And If You got some benefits from this article then please share it with your friends.

3 thoughts on “Sieve of Eratosthenes In Python”

Leave a Comment

Share via
Copy link
Powered by Social Snap