## finding the nth prime javascript

finding nth prime number javascript
find nth prime number
nth prime position node js
nth prime number node js
what is the 10001st prime number node js
prime number nth term
nth prime number in c++
c program to find nth prime number using recursion

the functions below are supposed to spit out the nth prime number. However, it keeps on spitting out 3. Can somebody please help? Cheers, Anthony

```function Prime(num) {
output = true
for (i=2 ; i<num ; i++) {
if (num%i === 0)  {
output = false ; break
}
}
return output
}

function PrimeMover(num) {
var count = 0
for (i=2 ; i<10000 ; i++)  {
if (Prime(i) === true) {
count = count + 1
}
if (count === num) {
return i
break
}
}
}
```

You have created loop counter `i` in `global scope`.so both `PrimeMover` and `Prime` mutates same global `i`.In every iteration ,PrimeMover assigns `i=2`.After that `Prime` assigns `i=2`.your `i` variable's value will be changed between `2` and `3`.use local loop counter variable `var i=0;`

```function Prime(num) {
output = true
for (var i=2 ; i<num ; i++) { //var i=2
if (num%i === 0)  {
output = false ; break
}
}
return output
}

function PrimeMover(num) {
var count = 0
for (var i=2 ; i<10000 ; i++)  { //var i=2
if (Prime(i) === true) {
count = count + 1
}
if (count === num) {
return i
break
}
}
}
```

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). This should return the nth prime (n being the number given by the user). It works fine for the first few numbers (1 returns 2, 2 returns 3, 3 returns 5) but when 5 is given, it returns 9 which isn't prime (it should be 11). This happens with other numbers as well above this (7 returns 15 when it should be 17).

```function main(inp) {
var count = 0;
for (var i = 2; i <= 100000; i++) {
if (isPrime(i)) count = count + 1;
if (count == inp) return i;
}
}
function isPrime(i) {
for (var j = 2; j < i; j++) {
//instead of `j < i` it can be reduced using other conditions
if (i % j == 0) {
return false
}
}
return true
}
main(5) // any number
```

felizbear's solution to Nth Prime on the JavaScript track, See how felizbear solved the Nth Prime exercise on the JavaScript track. I would find the code more self-explanatory if you moved the  Program to print first N Prime numbers; Maximum GCD of all subarrays of length at least 2; Count of subsets with sum equal to X using Recursion; Program to calculate Percentile of a student based on rank; Find the position of the given Prime Number; Count of numbers in range which are divisible by M and have digit D at odd places

This might be a bit more optimal

```function nthPrime(n) {
var P = 0;

function isPrime(x) {
var isPrime= true;

for (var d = 2; d <= Math.sqrt(x); d++) {
if((x/d) % 1 == 0) {
isPrime = false;
break;
}
}

return isPrime;
}

for (var i = 1; 0 < n; i++) {

if(isPrime(i)) {
P = i; n--;
}

// we can skip the even numbers
if(3 <= i){
i++;
}

}

return P;
}
```

Finding the nth prime - JSFiddle, Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor. Let’s see the steps we did to find the nth prime number in Java: import the Scanner class which is found in java. util package. Declaring and initializing some variables. Using the Scanner class to take input from the user.

For minimal code lovers,

```function nthprime(n)
{
var prime=[], i=1
while (i++ && prime.length<n-1) prime.reduce((a,c)=>(i%c)*a,1) && prime.push(i)
return prime.length?prime.pop():1
}
[1,2,3,5,10,100].forEach(n=>console.log(`nthprime(\${n})=\${nthprime(n)}`))```

[JavaScript] nth Prime Number Algorithm : learnprogramming, I'm working on a Coderbyte challenge to find the nth prime number. I've gotten to a place, or at least thought I did, where I can collect prime numbers in an array. The logic is simple. First, you take input from the user asking the value of n. Then you run a loop finding all the prime numbers. Whenever a prime number is found, the count is increased and if the count is equal to the input of user (i.e., if the prime number found is the nth prime number), then print it.

Find the nth Prime Number, Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Next Step Leave tour. HTML CSS JS Result. HTML. HTML. HTML Options. Format HTML There are four prime digits 2, 3, 5 and 7. First observation is that the number of numbers of x length and made of prime digits are because for each position you have 4 choices so total number is 4^x.

Nth Prime JS, Failed to connect, retrying. Code Console Commands. @silybum/. Nth Prime JS. JavaScript. No description. sign up. No File Open. Nth prime Here's how it works: Enter a value for n below, from 1 to 10 12 , inclusive. The server will return the n th prime number (counting 2 as the first).

Finding nth prime number – JavaScript – bckurera's thoughts, The source code can be used to find out any nth prime. However the time efficiency of this algorithm is in order of n! which means for higher upper  Finding the nth prime number using function in "C" Code with C | Programming: Projects & Source Codes › Forums › C and C++ › Finding the nth prime number using function in "C" This topic has 3 replies, 1 voice, and was last updated 4 years, 2 months ago by Amit .

• You need to use local variables. Otherwise, all of your loops will overwrite each other! Take a look at my prime number programming interface here: myersdaily.org/joseph/javascript/primes-10k.2.html in which you can do what you want with code like this `var t = [], i=2, n = 300; while (t.length < n) { prime(i) && (t[t.length] = i); i++; } t[t.length-1]; // our list` then just click "Execute" (outputs 1987)
• Instead of using an `output` variable, can you not simply return `false` in the loop and return `true` at the end? I think that would be easier.