Sieve of Eratosthenes

Sieve of Eratosthenes is a famous simple algorithm use to find all the primes nos. upto certain limit.

Algorithm:

  1. Intialize an array of size n with all the values 1(where n is limit upto which prime number needed).
  2. Set arr[0] and arr[1] as 0 as they are not prime.
  3. Intialize p as 2.
  4. While p*p is less than equal to n.
    1. If arr[p]==1
      1. Intialize j to p+p
      2. while j is less than equal to n
        1. arr[j]=0
        2. j=j+p
    2. p=p+1