Python Program for Sieve of Eratosthenes

22 Jun, 2022
Sieve of Eratosthenes is a method for finding all primes up to (and possibly including) a given natural. This method works well when is relatively small, allowing us to determine whether any natural number less than or equal to is prime or composite. 
Implementation:
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For instance here if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”.
Example 
Python3
# Python program to print all Primes Smaller# than or equal to N using Sieve of Eratosthenes  def SieveOfEratosthenes(num):    prime = [True for i in range(num+1)]# boolean array    p = 2    while (p * p <= num):         # If prime[p] is not        # changed, then it is a prime        if (prime[p] == True):             # Updating all multiples of p            for i in range(p * p, num+1, p):                prime[i] = False        p += 1     # Print all prime numbers    for p in range(2, num+1):        if prime[p]:            print(p)  # Driver codeif __name__ == '__main__':    num = 50    print("Following are the prime numbers smaller"),    print("than or equal to", num)    SieveOfEratosthenes(num)
Output: 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Following are the prime numbers below 30
2 3 5 7 11 13 17 19 23 29
Time Complexity: O(n*log(log(n)))
Auxiliary Space: O(n)
Python Program for Sieve of Eratosthenes | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersPython Program for Sieve of Eratosthenes | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:02•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Jm5aQoMmb1E" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please refer complete article on Sieve of Eratosthenes for more details! 
SonalSrivastava02
itsmayank0
brendansport
amartyaghoshgfg
abhishek20b1531008
aryash747
chandramauliguptach
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python | Convert string dictionary to dictionary
Python program to add two numbers
Python Program for factorial of a number
Iterate over characters of a string in Python
Python program to find second largest number in a list
Python | Convert set into a list
Python | Convert a list into a tuple
Python Program for Bubble Sort
Python program to find sum of elements in list
Appending to list in Python dictionary