Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the nth prime number in JavaScript
In this article, we'll learn how to find the nth prime number using JavaScript. A prime number is a positive integer greater than 1 that has no divisors other than 1 and itself.
Understanding the Problem
We need to create a JavaScript function that takes an input n and returns the nth prime number. For example, if n=5, we should return 11 (the 5th prime number in the sequence: 2, 3, 5, 7, 11).
Algorithm Approach
We'll use a two-step approach:
-
Helper Function: Create an
isPrime()function to check if a number is prime - Main Function: Use a counter to find the nth prime by testing consecutive numbers
Implementation
Step 1: Prime Number Checker
The isPrime() function checks divisibility from 2 up to the square root of the number:
function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
// Test the function
console.log(isPrime(2)); // First prime
console.log(isPrime(17)); // Prime number
console.log(isPrime(15)); // Not prime
true true false
Step 2: Finding the nth Prime
function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
function findNthPrime(n) {
let count = 0;
let num = 2;
while (count < n) {
if (isPrime(num)) {
count++;
}
num++;
}
return num - 1;
}
// Examples
console.log("5th prime:", findNthPrime(5));
console.log("10th prime:", findNthPrime(10));
console.log("25th prime:", findNthPrime(25));
5th prime: 11 10th prime: 29 25th prime: 97
How It Works
- isPrime(): Returns false for numbers ? 1, then checks divisibility from 2 to ?num
- findNthPrime(): Starts from 2, counts prime numbers until reaching the nth prime
- Counter Logic: Increments count when a prime is found, increments num each iteration
- Return Value: Returns num - 1 because num is incremented after finding the nth prime
Complete Example with Multiple Tests
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
function findNthPrime(n) {
let count = 0;
let num = 2;
while (count < n) {
if (isPrime(num)) count++;
num++;
}
return num - 1;
}
// Test with first 10 primes
console.log("First 10 primes:");
for (let i = 1; i <= 10; i++) {
console.log(`${i}th prime: ${findNthPrime(i)}`);
}
First 10 primes: 1th prime: 2 2th prime: 3 3th prime: 5 4th prime: 7 5th prime: 11 6th prime: 13 7th prime: 17 8th prime: 19 9th prime: 23 10th prime: 29
Time and Space Complexity
- Time Complexity: O(n × ?m), where n is the position and m is the nth prime number
- Space Complexity: O(1), using only a few variables
Conclusion
This solution efficiently finds the nth prime number using a helper function to check primality and a main function to iterate through numbers. The algorithm is straightforward and works well for moderate values of n.
