-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_euler_prob7.c
58 lines (50 loc) · 1 KB
/
project_euler_prob7.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
53
54
55
56
57
58
/*
* project_euler_prob7.c
*
* Created on: Jul 20, 2012
* Author: ssimmons
*
* Find the 10,001st prime number
*
* "Sift the Two's and Sift the Three's,
* The Sieve of Eratosthenes.
* When the multiples sublime,
* The numbers that remain are Prime."
* --Anonymous
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LENGTH 500000
int main(){
int *candidates = (int * ) calloc(sizeof(int)*LENGTH,sizeof(int));
int i, j;
j = 0;
// Initialize array
candidates[0] = 0;
candidates[1] = 0;
for (i = 2; i<LENGTH; i++)
candidates[i] = 1;
// Do sieving such that all 'true' (== 1) are now prime
for (i = 2; i< ( (int) ceil(sqrt(LENGTH)) ); i++){
if(candidates[i] == 1){
for(j=(i*i); j<LENGTH; j += i){
candidates[j] = 0;
}
}
}
// Now count # of primes up to 10,001st
j = 0;
for (i = 2; i< LENGTH; i++){
if(candidates[i] == 1){
j++;
if(j == 10001){
printf("The 10001st prime is: %d", i);
break;
}
}
}
free(candidates);
return 0;
}