-
Notifications
You must be signed in to change notification settings - Fork 0
/
p7.c
52 lines (35 loc) · 946 Bytes
/
p7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#define N 10001
int isprime(int, const int*, int);
int getprime(int);
/* Problem:
print the 10,001st prime number.
*/
int main(void) {
printf("%d", getprime(N));
return 0;
}
int isprime(int n, const int* primes, int size) {
// Returns true if n is a prime number
int i;
for(i = 0; i < size; i++) {
if(!(n % *(primes + i))) return 0;
}
return 1;
}
int getprime(int n) {
// Returns the n-th prime number
int* primes = malloc(n * sizeof(primes)), i, count = 2;
if(!primes) exit(-1); // allocation error
*primes = 2;
*(primes + 1) = 3; // load some primes
while(count != n) {
i = *(primes + count - 1) + 2; // i = [last prime] + 2 (since, primes must be odd except for 2)
while(!isprime(i, primes, count)) i += 2;
*(primes + count++) = i;
}
int p = *(primes + n - 1);
free(primes);
return p;
}