Determining Prime Numbers in JavaScript: Logic and Code Optimization

Master prime number detection in JavaScript! Level up your coding skills with optimized algorithms. Explore the code snippets and unleash your mathematical prowess. #JavaScriptPrimeNumbers #CodingSkills

Theodore John.S
3 min readJul 14, 2023

In this article, we will explore how to write a JavaScript function that can determine whether a given number is prime. We will explain the logic behind the function and provide a simple code snippet. Additionally, we will present an optimized code snippet that improves both time and space complexity.

Photo by Alan Carrillo on Unsplash

Logic:

To determine whether a given number is prime, we can follow a basic algorithm. Prime numbers are numbers greater than 1 that have no positive divisors other than 1 and themselves. Based on this, we can iterate from 2 to the square root of the given number and check if any of these values evenly divide the number. If a divisor is found, the number is not prime. Otherwise, it is prime.

Simple Code Snippet:

Here's a simple code snippet that implements the logic described above:

function isPrime(number) {
if (number < 2) {
return false;
}

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

return true;
}

// Usage
const number = 17;
console.log(`${number} is prime:`, isPrime(number));

This code snippet starts by checking if the given number is less than 2. If it is, the function immediately returns false, as prime numbers must be greater than 1. Next, it iterates from 2 up to the square root of the number, checking if any of these values divide the number evenly. If a divisor is found, the function returns false, indicating that the number is not prime. Otherwise, it returns true.

Optimized Code Snippet:

Although the simple code snippet is effective for most cases, it can be optimized further by considering specific scenarios and reducing the number of iterations.

function isPrimeOptimized(number) {
if (number < 2) {
return false;
}

if (number === 2 || number === 3) {
return true;
}

if (number % 2 === 0 || number % 3 === 0) {
return false;
}

for (let i = 5; i * i <= number; i += 6) {
if (number % i === 0 || number % (i + 2) === 0) {
return false;
}
}

return true;
}

// Usage
const number = 17;
console.log(`${number} is prime:`, isPrimeOptimized(number));

The optimized code snippet includes additional checks before entering the loop. It handles cases where the number is 2 or 3 as prime numbers, reducing unnecessary iterations. It also checks if the number is divisible by 2 or 3, which are the only even prime numbers, to further optimize the algorithm. Finally, it iterates in steps of 6, checking if the number is divisible by i or i + 2. This approach reduces the number of iterations and improves both time and space complexity.

Conclusion:

In this article, we discussed the logic behind determining whether a given number is prime using JavaScript. We provided a simple code snippet that iterates up to the square root of the number, checking for divisors. Additionally, we presented an optimized code snippet that considers specific scenarios and reduces unnecessary iterations, improving both time and space complexity. By understanding and implementing these code snippets, you can efficiently determine whether a number is prime in JavaScript.

--

--

Theodore John.S

Passionate self-taught front-end dev. HTML, CSS, JS, React | Creating pixel-perfect web experiences |🌐Find me on LinkedIn: https://www.linkedin.com/in/stj/