# Sieve of Eratosthenes in Python

**URL:** https://discourse.processing.org/t/sieve-of-eratosthenes-in-python/37866
**Category:** Processing.py
**Created:** [July 7, 2022, 7:12am UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-in-python/37866 "2022-07-07T07:12:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![shivambhatele](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shivambhatele/32/16467_2.png) [@shivambhatele](https://discourse.processing.org/u/shivambhatele)
#### Post date: [July 7, 2022, 7:12am UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-in-python/37866/1 "2022-07-07T07:12:28Z")

</div>

Hello All, I working on a python project and I am confused sieve of eratosthenes coding problem. The problem statement is Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.

A prime number is a number that is divisible by only two numbers – themselves and 1

Example:  
Input: n =10  
Output: 2 3 5 7

I have taken this code reference from [here](https://www.interviewbit.com/blog/sieve-of-eratosthenes/). Can anyone explain me with the help of this code, how sieve of eratosthenes program works? or explain with another example?

```auto
def isPrime(n):
     
    # Corner case
    if n <= 1 :
        return False
 
    # check from 2 to n-1
    for i in range(2, n):
        if n % i == 0:
            return False
 
    return True
 
# Function to print primes
def printPrime(n):
    for i in range(2, n + 1):
        if isPrime(i):
            print(i, end = " ")

```

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [July 7, 2022, 9:28am UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-in-python/37866/2 "2022-07-07T09:28:58Z")

</div>

This is a Processing forum, so here’s something that uses Processing.py / Python Mode, that rearranges the example in a way you may find easier to comprehend –

```python
# set framerate to 1 frame every 2 seconds
def setup(): frameRate(0.5)

def draw():
    # loop through all of the numbers between 2 and framecount
    for i in range(2, frameCount):
        # divide the framecount by every number from 2 up to framecount
        if frameCount % i == 0:
            # if the remainder of any division is zero, 
            # the framecount divides evenly by i, 
            # therefore it is not a prime number
            print(
'{} is not prime because it divides evenly into {}'.format(frameCount, i)
            )
            # exit the loop immediately and start again on the next frame
            return
    # if the loop didn't exit, the framecount is a prime number
    print('{} is prime'.format(frameCount))

```

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [July 7, 2022, 10:11am UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-in-python/37866/3 "2022-07-07T10:11:10Z")

</div>

You may find this discussion interesting, which is on [Python Software Foundation Discourse](https://discuss.python.org/):

> **[Sieve of Eratosthenes in Python](https://discuss.python.org/t/sieve-of-eratosthenes-in-python/17130/)**
>
> Hello All, I working on a python project and I am confused sieve of eratosthenes coding problem. The problem statement is Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. I am trying to solve...
