Sieve of Eratosthenes
Sieve of Eratosthenes is a famous simple algorithm use to find all the primes nos. upto certain limit.
Algorithm:
-
Intialize an array of size n with all the values 1(where n is limit upto which prime number needed).
-
Set arr[0] and arr[1] as 0 as they are not prime.
-
Intialize p as 2.
-
While p*p is less than equal to n.
- If arr[p]==1
- Intialize j to p+p
- while j is less than equal to n
- arr[j]=0
- j=j+p
- p=p+1