By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
答案:104743
#include #include #include
int isPrime(int n) { if(1 == n) return 0; else if(4 > n ) return 1; else if(0 == (n%2)) return 0; else if(9 > n) return 1; //4, 6, 8 is excluded by row 7; else if(0 == (n%3)) return 0; else { int r = ( int)floor(sqrt((double)n)); int f = 5; while(f <= r){ if(0 == (n%f)) return 0; if(0 == (n%(f+2))) return 0; f += 6; } return 1; } }
int main(void) { const int limit = 10001; int count=1; int n = 1; do{ n += 2; if(isPrime(n)) count++; }while(count < limit); printf("%d\n", n); return 0; }