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 10001st prime number?
- #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <limits.h>
-
#include <math.h>
-
-
#define NUM 10001
-
int prime[NUM];
-
-
#if 0
-
int is_prime(int num)
-
{
-
int i;
-
-
int sqrt_num = 1+sqrt(num);
-
for (i = 3; i < sqrt_num; i+=2) {
-
if (!(num%i))
-
return 0;
-
}
-
-
return 1;
-
}
-
-
#else
-
int is_prime(int num)
-
{
-
int i;
-
int sqrt_num = sqrt(num);
-
-
for (i = 0; i < NUM; i++) {
-
if (prime[i]) {
-
if (!(num % prime[i]))
-
return 0;
-
else if (prime[i] > sqrt_num)
-
break;
-
-
} else
-
break;
-
}
-
-
return 1;
-
}
-
#endif
-
-
int main(int argc, const char *argv[])
-
{
-
int nr = 2;
-
int i;
-
-
memset(prime, 0, sizeof(prime));
-
prime[0] = 2;
-
prime[1] = 3;
-
-
for (i = 5; i < (INT_MAX); i+=2) {
-
if (is_prime(i)) {
-
prime[nr] = i;
-
if (++nr == NUM)
-
break;
-
-
}
-
}
-
-
printf("result: %d\n", i);
-
-
return 0;
-
}
阅读(1111) | 评论(0) | 转发(0) |