Finding the nth prime number in JavaScript


In the given problem statement our task is to find the nth prime number with the help of Javascript functionalities. So we will use two functions to solve this problem.

Understanding the Problem

The problem at hand is to find the nth prime number using the Javascript programming. So the prime number is a positive integer number which is greater than 1 and which has no positive divisors other than 1 and the number itself. The task is to create a Javascript function which takes an input n. Here n represents the position of the prime number which is to be found.

Logic for the given Problem

To solve this problem we will use a two step approach. In the first approach, we need to define a helper function to check that the number is a prime number. In this function we will check if the number has any divisors other than 1 and the number itself. The function will iterate from 2 to the square root of the number and check if there are any divisors.

In the second approach we will define the main function with the help of it we will find the nth prime number. In this function we will initialize a counter variable to keep track the number of prime numbers found so far. And we will initialize the numbers to 2. We will use a while loop to iterate until count reaches to n. During the iteration we will check that the number is prime by calling the above function. If the number is a prime number then we will increment the count variable. And we will return the value of the number.

Algorithm

Step 1: As we have to find the nth prime number so for doing this task we will define a function to check that the number is prime or not. The name of the function will be isPrime and inside the function we will pass a parameter as num.

Step 2: Inside the above function we will check that the num is less than or equal to 1. If this condition is true then we will return false means the number is not a prime number.

Step 3: Now we will use a for loop and this loop will run until the Math.sqrt(num). Inside this loop we will check another condition: if the num is divided by the current number and the remainder is zero then we will return false. otherwise true.

Step 4: After that we will define the main function to find the nth prime number. And name this function as findNthPrime inside this function we will pass a parameter as n.

Step 5: First we will declare two variables count and num and initial values of these variables will be 0 and 2 respectively.

Step 6: So in this step we will use a while loop and run this loop until the value of count is less than n. In this loop we will check whether the num is a prime number or not. If this condition is true then increment the value of count by 1. Otherwise increment the value of num by 1.

Step 7: And finally return the final value as num - 1.

Example

//Function to check the number is 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 to find the nth prime number
  function findNthPrime(n) {
   let count = 0;
   let num = 2;
   while (count < n) {
     if (isPrime(num)) {
      count++;
     }
     num++;
   }
   return num - 1;
  }
 
  console.log(findNthPrime(10));
  console.log(findNthPrime(100));

Output

29
541

Complexity

The time complexity for finding the nth prime number is O(n * sqrt(num)), in which num is the input number and n is the position of the prime number. The reason for this complexity is that the isPrime function has time complexity of O(sqrt(num)) and the findNthPrime function has a time complexity of O(n * sqrt(num)) because this function calls isPrime function. The space complexity of the code is O(1) as the code uses a few variables to store the intermediate values.

Conclusion

The provided code effectively found the nth prime number. We have used a helper function to check that the number is prime and also a main function to iterate over the numbers until we have not found the nth prime number.

Updated on: 14-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements