No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Sieve of Eratosthenes In Python

by Khaleel O.
January 7, 2022
in Python
Reading Time: 2 mins read
A A
sieve of eratosthenes in python
Sieve Of Eratosthenes Prime Numbers Python

Let’s code the Sieve Of Eratosthenes algorithm for prime numbers using Python. We’ll be using Python 3.8.10. Let’s go! ⚡⚡✨✨

Eratosthenes was a mathematician that lived in Ancient Greece. The Sieve Of Eratosthenes is an algorithm he developed that allows us, even today, to efficiently find the prime numbers less than any integer n. Here is our code:

def esieve(n):
    multiples = []
    for i in range(2, n+1):
        if i not in multiples:
            print(i)
            for j in range(i*i, n+1, i):

                multiples.append(j)

esieve(17)

#Output
#2
#3
#5
#7
#11
#13
#17

Let’s explain what is happening here:

  1. Our function esieve() is what will actually implement the Sieve of Eratosthenes algorithm. It takes one argument n and will print all of the prime numbers less than or equal to n. These are the steps:
    • we declare a new list multiples
    • we generate a list of integers from 2 to n+1
    • starting at i=2 we “mark” all the multiples of i in the list by inserting each multiple into our list multiples, up to n+1
    • every time we finish this loop we check to see if i is in the list multiples, if not, it is a prime number and we print it to the screen
    • then we increment to i=3 and run through all the numbers again, each time inserting multiples of i into our list multiples and checking to see if, after the loop is done, it is in the list multiples
    • we proceed by incrementing i in this manner and doing this “marking” until n+1 at which point the process is complete and the sieve has done its job
  2. We modified the sieve to include primes up to and including n.

Simple right? There are far more fancy ways to do this but it is always good to practice and produce readable code. The other solutions look more elegant but they aren’t readable.

Find out more about this algorithm HERE. And check our another great tutorial HERE. Thanks for reading! Good luck! 👌👌👌

Tags: fibonacciprimes
Previous Post

ROT13 Python Code

Next Post

Python dnspython Introduction

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy