Coding : Identifying Prime Numbers within the given Number in JavaScript

Monu Kumar Modi
The Fresh Writes
Published in
3 min readJan 22, 2023

--

Prime numbers have always been an interesting topic in mathematics, and their importance extends beyond just academic curiosity. They are used in various fields such as cryptography and computer science. In this blog post, we will explore how to efficiently identify prime numbers in JavaScript using a simple function.

First, let’s take a look at the isPrime() function. This function takes in a number as a parameter and returns a boolean value of either true or false, depending on whether the number is prime or not.

function isPrime(num) {
if (num < 2) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

The function starts by checking if the number is less than 2, and if it is, it returns false as any number less than 2 is not considered a prime number. If the number is greater than or equal to 2, the function then checks for divisibility by any number between 2 and the square root of the number. If the number is divisible by any of these numbers, the function returns false, as the number is not prime. If the number is not divisible by any of these numbers, the function returns true, indicating that the number is prime.

Now that we have the isPrime() function to check for prime numbers, we can use it in another function called findPrimes(). This function takes in a number as a parameter and returns an array of all prime numbers between 2 and that number.

function findPrimes(n) {
const primes = [];
for (let i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
return primes;
}

The findPrimes() function starts by declaring an empty array called primes. It then uses a for loop to iterate through all numbers between 2 and the input number. For each number, it checks if the number is prime using the isPrime() function, and if it is, it adds it to the primes array. After the for loop has completed, the function returns the primes array, which now contains all prime numbers between 2 and the input number.

Complete Code: -

function isPrime(num) {
if (num < 2) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

function findPrimes(n) {
const primes = [];
for (let i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
return primes;
}

console.log(findPrimes(20)); // [2, 3, 5, 7, 11, 13, 17, 19]

Here’s an example of how the two functions can be used together:

console.log(findPrimes(20)); // [2, 3, 5, 7, 11, 13, 17, 19]

This will output an array of prime numbers between 2 and 20, which are [2, 3, 5, 7, 11, 13, 17, 19].

In conclusion, identifying prime numbers in JavaScript can be done easily and efficiently using the above-described functions. The isPrime() function checks for the primality of a single number, while the findPrimes() function finds all prime numbers between 2 and a given number. These functions can be used in various applications, such as cryptography and computer science, where prime numbers play an important role.

If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.

Thanks for reading.Happy learning 😄

Do support our publication by following it

Also refer to the following articles.

--

--