Skip to content

Instantly share code, notes, and snippets.

@MuddyBootsCode
Created April 27, 2022 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MuddyBootsCode/e0e1941f901880837aa74580a1c3b42d to your computer and use it in GitHub Desktop.
Save MuddyBootsCode/e0e1941f901880837aa74580a1c3b42d to your computer and use it in GitHub Desktop.
function nthPrime(n) {
let count = 0;
let current = 2;
let prime = 2;
while(count < n){
let isPrime = true;
for(let i = 2; i < current; i++){
if(current % i === 0){
isPrime = false;
break;
}
}
if(isPrime){
prime = current;
count += 1;
}
if(current === 2){
current = 3;
} else {
current += 2;
}
}
return prime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment