Sieve of Eratosthenes is used to get all prime number in a given range and is a very efficient algorithm. You can check more about sieve of Eratosthenes on Wikipedia. It follows the following steps to get all the prime numbers from up to n:
- Make a list of all numbers from 2 to n.
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ……., n] - Starting from 2, delete all of its multiples in the list, except itself.
[2, 3,4, 5,6, 7,8, 9,10, 11,12, 13,14, 15,16, 17,18, 19,20,……., n] - Repeat the step 2 till square root of n.
For 3 – [2, 3,4, 5,6, 7,8,9,10, 11,12, 13,14,15,16, 17,18, 19,20……., n]
For 5 – [2, 3,4, 5,6, 7,8,9,10, 11,12, 13,14,15,16, 17,18, 19,20……., n]
Till sqrt(n)
The remaining list only contains prime numbers.
Example of first 100 prime numbers
- Step1: Initialize the list – [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
- Step2 and step3:
For 2 – [2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
For 3 – [2, 3, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97]
For 4 – (Not in list, deleted during deletion of multiples of 2)
For 5 – [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 77, 79, 83, 89, 91, 97]
For 6 – (Not in list, deleted during deletion of multiples of 2)
For 7 – [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
For 8 – (Not in list, deleted during deletion of multiples of 2)
For 9 – (Not in list, deleted during deletion of multiples of 3)
For 10 [last] (Square root of 100 is 10) – (Not in list, deleted during deletion of multiples of 2) - Final list – [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Python Code
import math
print ("Enter the a number")
number = int(input())
primes = []
for i in range(2,number+1):
primes.append(i)
i = 2
#from 2 to sqrt(number)
while(i <= int(math.sqrt(number))):
#if i is in list
#then we gotta delete its multiples
if i in primes:
#j will give multiples of i,
#starting from 2*i
for j in range(i*2, number+1, i):
if j in primes:
#deleting the multiple if found in list
primes.remove(j)
i = i+1
print (primes)
This code is a straightforward representation of the steps given above. We are first iterating from 2 to the square root of the number at the end of the list using a variable ‘i’ and if the value of ‘i’ is found in the list, then we have to delete all its multiples. This is done using the for loop. range(i*2, number+1, i)
– This will start from 2*i and will take a step of ‘i’ for the next iteration (2*i, 3*i, 4*i, etc.). If this number is found in the list then we are deleting it using primes.remove(j)
.
Reference – https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes