Using Sieve of Eratosthenes to find primes JavaScript


In this problem statement, our aim is to apply a sieve of eratosthenes algorithm to find out the prime numbers with the help of Javascript functionalities. So in our program we will implement a function to find primes numbers up to a given limit.

Understanding the problem statement

The problem statement is to write a function in Javascript that will work for finding the prime numbers up to a given number. And for implementing this function we will use the Sieve of Eratosthenes algorithm.

What is the Sieve of Eratosthenes Algorithm ?

The Sieve of Eratosthenes is an easy and classical algorithm for finding all prime numbers up to a given range. This algorithm works by defining a list of all numbers from 2 to the given range and then iteratively mark the multiples of every prime number as not prime. This procedure will continue until all the prime numbers have been identified.

This algorithm is an efficient algorithm for finding prime numbers with a time complexity O(n log n). So we can say that this method is much faster than the brute force technique for checking every number for primality.

Implementation for the given problem

To implement this algorithm we will create an boolean array of size n+1, in which every index will represent a number from 0 to n. All the elements in the array are initially set to true. This will indicate that every number is prime. The first two elements that are 0 and 1 are set to false because they are not prime numbers.

So after that, starting from the first prime number that is 2, the algorithm will loop through the array and will mark all the multiples as composite numbers by setting their values in the array to false. Now the algorithm will move on to the next unmarked number and will repeat the procedure until all the prime numbers have been found.

At the end the algorithm will loop through the array and collect all the indexes that have a value to true. Which indicates that they are prime numbers.

Algorithm

Step 1 − Create a function generatePrimes to find prime numbers up to n numbers. Here n is the parameter passed in the function.

Step 2 − Create a n+1 sized array, this array is a boolean type and initialize all its values to true.

Step 3 − Loop through the array from 2 to the square root of n. And check if the current number is prime means the value is true then loop through all of its multiplies up to n and set their values in the array to false

Step 4 − Again loop through the array from 2 to n and add all prime numbers to a result array.

Step 5 − Return the result array of prime numbers and show the example usage on the screen.

Code for the algorithm

function sieveOfEratosthenes(n) {
   // Create a boolean array of size n+1
   let primes = new Array(n + 1).fill(true);
   // Set first two values to false
   primes[0] = false;
   primes[1] = false;
   // Loop through the elements
   for (let i = 2; i <= Math.sqrt(n); i++) {
      if (primes[i]) {
         for (let j = i * i; j <= n; j += i) {
            primes[j] = false;
         }
      }
   }

   let result = [];
   // Loop through the array from 2 to n
   for (let i = 2; i <= n; i++) {
      if (primes[i]) {
         result.push(i);
      }
   }
    
   return result;
}
console.log("All prime numbers up to 20");
console.log(sieveOfEratosthenes(20));

Complexity

The time taken by the created function is O(n log log n), in which n is the size of the boolean array. The algorithm iterates from 2 to the square root of n and it marks all the multiples of every prime number as composite. And the number of loop iterations is equal to n log log n, so the resulting time complexity will be O(n log log n). So this algorithm is very efficient for finding the prime numbers. The space complexity is O(n).

Conclusion

Sieve of Eratosthenes algorithm is a systematic approach for finding the prime numbers. It works by finding all non prime numbers up to a given limit and also marks as composite numbers. So the remaining numbers will be prime numbers. This algorithm is particularly effective for small to medium sized numbers and very easy to implement in the code.

Updated on: 18-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements