Skip to content

Instantly share code, notes, and snippets.

@heejune
Created June 4, 2017 15:28
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 heejune/c351b1de6b4c4180d0ed17172f6c504d to your computer and use it in GitHub Desktop.
Save heejune/c351b1de6b4c4180d0ed17172f6c504d to your computer and use it in GitHub Desktop.
unsigned long get_nth_prime_without_sieve(unsigned long n) {
if (n == 0) return 0;
if (n == 1) return 2;
unsigned long prime_num = 3;
unsigned long local_n = 2; // 2th prime number is 3
while (local_n != n) {
// check next prime number candidate
prime_num += 2; // 3,5,7,9... check only odd numbers
if (isPrime(prime_num) == true) {
local_n++;
}
}
return prime_num;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment