Explain how this code to find the nth prime works

Hello! I’m a beginner at learning Javascript and found a code to find the nth prime number given a user’s input (ex: find the 10th prime number). However, I was wondering if someone could explain explain why/how this Javascript code works?

Thanks!

function start()
{
    findNthPrime();
}

function findNthPrime()
{
    var n = readInt();
    var num = 1;
    var count = 0;
    
    while (count < n)
    {
        num = num+1;
        for (var i = 2; i <= num; i++)
        {
            if (num % i == 0)
            {
                break;
            }
        }
        if (i == num)
        {
            count = count+1;
        }
    }
    println("Your nth prime is: " + num);
}

Welcome to the forums @Fishhhh

The method readInt and println appear to be Java rather than Javascript. I have therefore moved your post to General Web Dev.

1 Like

First of all, this looks more like java than javascript. Are you using some libraries or something which uses java-like syntax? println and readInt are not standard javascript objects.

That being said, it’s pretty straightforward if you break it down.

As you (hopefully) know, a prime number is any whole number greater than 1 that is only divisible by itself and 1.

So 2,3,5,7,11,13,17,19,23 so 2 is the 1st prime number, 3 is the 2nd prime number and so on…

So this function allows you to enter a number which corresponds to to find the appropriate value in the prime number list

This first line allows for the entry of a value (1, 2, 3,…100). The second and third lines are just initializers

    var n = readInt("Enter the term (ex: 1st prime number, 2nd prime number, etc...) to compute the nth prime number: ");
    var num = 1;
    var count = 0;

now it comes to the crux of the processing. Basically, this while loop is use the inner for loop to find each prime in the number in that series. Each loop through the count will start with the previous prime number to find the next one.

Let’s step through this. Let’s say you want to find the third prime number

It will enter the while loop. Count will be 0 because that’s how many primes have been found so far, n will be 3.

  • count is = 0 and 0 < 3 so continue.
    • num gets set to 2 (it’s the first prime, so it makes sense to start there)
    • the for loop will start with i = 2
      • it checks the remainder of 2 divided by 2. Because it is 0, the loop breaks
    • is i equal to num? i was 2, num was 2, so increment count this is the first prime
  • back to top of while statement - count is = 1 and 1 < 3 so continue.
    • num gets set to 3
    • the for loop will start with i = 2
      • it checks the remainder of 3 divided by 2. Because it is 1, the loop continues and i becomes 3. Since 2 < 3, continue the loop.
      • it checks the remainder of 3 divided by 3. Because it is 0, the loop breaks
    • is i equal to num? i was 3, num was 3, so increment count this is the second prime
  • back to top of while statement - count is = 2 and 2 < 3 so continue.
    • num gets set to 4
    • the for loop will start with i = 2
      • it checks the remainder of 4 divided by 2. Because it is 0, the loop breaks
    • is i equal to num? i was 2, num was 4, so this is NOT a prime
  • back to top of while statement - count is = 2 and 2 < 3 so continue.
    • num gets set to 5
    • the for loop will start with i = 2
      • it checks the remainder of 5 divided by 2. Because it is 1, the loop continues and i becomes 3. Since 3 <= 5, continue the loop.
      • it checks the remainder of 5 divided by 2. Because it is 1, the loop continues and i becomes 3. Since 3 <= 5, continue the loop.
      • it checks the remainder of 5 divided by 3. Because it is 2, the loop continues and i becomes 4. Since 4 <= 5, continue the loop.
      • it checks the remainder of 5 divided by 4. Because it is 1, the loop continues and i becomes 5. Since 5 <= 5, continue the loop.
      • it checks the remainder of 5 divided by 5. Because it is 0, the loop breaks
    • is i equal to num? i was 5, num was 5, so increment count this is the third prime
  • back to top of while statement - count is = 3 and 3 = 3 so skip rest of while statement
  • the value in num is the appropriate prime, in this case, the third one.
2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.