
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- Sieve of Eratosthenes in java
- Python Program for Sieve of Eratosthenes
- Java Program to get prime numbers using the Sieve of Eratosthenes algorithm
- C++ Program to Implement Sieve of eratosthenes to Generate Prime Numbers Between Given Range
- Find the Number of Primes In A Subarray using C++
- Filtering out primes from an array - JavaScript
- JavaScript Program for Count Primes in Ranges
- Representing number as the power and product of primes in JavaScript
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- Find the sum of all Truncatable primes below N in Python
- Bitwise Sieve in C++
- Prime Factorization using Sieve O(log n) for multiple queries in C++
- Using Kadane’s algorithm to find maximum sum of subarray in JavaScript
- How to find the binomial coefficient of two integers using JavaScript?
- C++ Program to Implement Sieve of Atkin to Generate Prime Numbers Between Given Range
