We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Create array of 10001 primes once before starting the loop, then get N from array's index
/////////////// ignore above this line ////////////////////functionmain(){vart=parseInt(readLine());letprimesArr=[]leti=2while(primesArr.length<=10000){if(isPrime(i)&&!primesArr.includes(i))primesArr.push(i)if(Number(primesArr.length)<=10000)i++elsebreak}for(vara0=0;a0<t;a0++){varn=parseInt(readLine());console.log(primesArr[Math.floor(n)-1])}}functionisPrime(n){if(n<2)returnfalseletsqrt=Math.floor(Math.sqrt(n))for(leti=2;i<=sqrt;i++){if(n%i==0)returnfalse}returntrue}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #7: 10001st prime
You are viewing a single comment's thread. Return to all comments →
Solution in JavaScript:
Create array of 10001 primes once before starting the loop, then get N from array's index