-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem10.c
62 lines (49 loc) · 957 Bytes
/
problem10.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
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void mark(long *sieve, long len, long x) {
long i;
for (i = x + x; i < len; i += x) {
*(sieve + i) = 0;
}
}
/** Creates the sieve, non inclusive
* First value is the number of elements represented
*/
long *primeSieve(long limit) {
long *M = calloc(limit, sizeof(long));
long i, count;
long lower = (long)(sqrt(limit)) + 1;
for (i = 0; i < limit; i++) {
M[i] = 1;
}
for (i = 2; i < lower; i++) {
mark(M, limit, i);
}
*M = 0;
*(M + 1) = 0;
count = 0;
for (i = 0; i < limit; i++) {
if (*(M + i)) {
count++;
}
}
*M = count;
return (M);
free(M);
}
int main(int argc, char *argv[]) {
const char *lim = argv[1];
char *e;
long limit = strtol(lim, &e, 0);
long *primes = primeSieve(limit);
long long sum = 0;
long i;
for (i = 0; i < limit; i++) {
if (*(primes + i)) {
sum += i;
}
}
printf("The answer to problem 10 is %lld\n", sum);
return 0;
}