Problem 7: 10001st prime (issues with testing)

The challenge is telling me that I am not passing the last given test-case where nthPrime(10001) should return 104743. When I console.log nthPrime(10001) using my solution it gives me the correct output of 104743 so I’m not sure why it’s not letting me pass. Not sure what’s going on here…

Your code so far


function nthPrime(n) {
  let arr = [2]
  let counter = 0 

  for (let i = 3; counter < n-1; i++){
    if (arr.every((x)=> i%x !== 0)){
        arr.push(i)
        counter++
    }
  }
    return arr[n-1];
}

nthPrime(10001);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-7-10001st-prime

That usually means that the test is timing out so it is not completing that last (longest) test. That means you should rethink your solution to be more efficient.

1 Like

If i use hint solution - same result. The last test not passing

The hints are not closely maintained by FCC. See if you can find your own solution. I’m not saying it’s easy, but it’s worth it. Look for ways to make it more efficient.

I do think that there is something up with this test, not the tester per say, but the way infinite-loop protections are interfering with the tests.
I made 2 separate solutions, the first ‘sieve’ like using the every function and another using straight loops and optimized with the root, for the former, once I remove the infinite loop protection in repl.it, I can arrive at the nthPrime(10001) solution in ~200ms, and for the latter in ~9ms.
Both these times aren’t large enough to warrant a timeout, however both will not pass in replit without removing the infinite loop protection, and they certainly do not pass in the FCC test module.

Looking at repl.it’s infinite loops docs , they say:

So we came up with the following set of heuristics to handle those edgecases. A loop will throw an error if the following was true:

  1. It happens in a single run (doesn’t yield to the event loop)
  2. A high number of iterations
  3. The loop is taking too long

I don’t think 9ms or even 200ms can qualify for ‘taking too long’, so I’m assuming it is either because of 1 or 2, and if FCC’s tester employs a similar method of detecting infinite loops, then reducing the number of iterations even further maybe the only way to make this pass but I don’t see how that is possible, and/or it somehow detects that my loops have no end limit set (since they terminate on break) and it just terminates it… Not sure

My solution

function nthPrime(num){
  const start = Date.now()
  let primes =[2]
  while(primes.length < num){
    const newP = getPrime(primes);
    primes.push(newP)
  }
  console.log(Date.now()-start)
  return primes[primes.length - 1];
}

For the optimized one which processes in ~ 9ms

const getPrime = (primes) =>{
 let forward = primes[primes.length - 1] + 1;
 for (forward;;forward++){
   const root = Math.sqrt(forward)
   let foundPrime = true;
   for (let i=0; i < primes.length; i++){
     if (primes[i] <= root){
       if(forward % primes[i] === 0) {
         foundPrime=false;
         break;
       } else continue;
     }
     else break;
   }
   if(foundPrime) break;
 }
 return forward;
}

The non-optimized one processing in ~200ms

getPrime = (primes) => {
 let forward = primes[primes.length - 1] + 1;
 let noPrime = false;
 while(!noPrime){
   const inDivisible = primes.every(p => forward % p !== 0)
   if(inDivisible) break;
   forward += 1;
 }
 return forward;
}

It is passable. It just did it with my solution.

Yes, it is difficult. Yes, it will take a lot of optimization. But it is doable. Correct me if I’m wrong, but if no cert depends on this. But this is great training for your coding brain. Knowing a programming language isn’t the only thing to being a programmer. Being able to wrap your head around things like this is a big part of it too. Every solution you come up with is making you better.

1 Like

Understood, and agree it is worth the while struggling with these problems, but if i’m solving it under <10ms it is no longer a question of efficiency but rather efficacy (of the code) ?

I guess I don’t understand what you’re asking. Your code is passing.

And any code that can find the solution regardless of time is efficacious. If you’re worried about how fast it can do it, then it’s efficiency. But if your code is passing the FCC test, then it must be pretty good.

It is not passing the last FCC test but it passes in repl.it if I disable the infinite loop protection, and it does so under 10ms, so the infinite loop protection is not being turned on because the loop is taking too long but some for other factor, which I’m not sure of… it also solves under 10ms in my local node insatllation too…

I was able to get your code to pass for me. What is the exact code you are trying?

It passed? Hmmm… must be a browser setting kicking on the infinite loop switch , I’m on chrome.
Bother versions of my code that I pasted above fail on my browser for the last part of the problem.
Well, thanks for looking into it,

The 9ms one passed for me. I didn’t try the other. It might be browser, and it might be processor speed. You might also try clearing your browser cache or trying a different browser for just this test. Or don’t worry about it and move on.

That is the problem with browsers, just change your browser to google chrome for this challenge.

Yeah I’ve given up, can’t dick with it any longer, but at-least I know it works on some machines, thanks!