Skip to content

Commit 8fe98de

Browse files
authored
Merge pull request #139 from kushchoudhary98/main
add: sieve_of_eratosthenes.py
2 parents 91420c7 + 289decf commit 8fe98de

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Math/sieve_of_eratosthenes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Sieve Of Eratosthenes:
2+
The sieve of eratosthenes is one of the most efficient way to find all
3+
the prime numbers upto the number `n`
4+
5+
for more reference(https://www.geeksforgeeks.org/sieve-of-eratosthenes/)
6+
"""
7+
8+
#importing `math` module which will be used later
9+
import math
10+
11+
#specify upto where you have to find the prime numbers
12+
n = int(input("Enter the range : "))
13+
14+
#`arr` is a boolean list that contains `n+1` `False` entries
15+
arr = [False]*(n+1)
16+
17+
#loop upto the square root of the range `n`
18+
for i in range(2,int(math.sqrt(n))+1):
19+
if arr[i] == False:
20+
for j in range(i*i, n+1, i):
21+
#making the entry `True` for all entries whose index is the multiple
22+
arr[j] = True
23+
24+
#after the loop exits, all the entry that are prime numbers
25+
#are marked as `False`
26+
27+
#printing all the prime numbers
28+
for i in range(2,n):
29+
if arr[i+1] == False:
30+
print(i+1)

0 commit comments

Comments
 (0)