-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
sol2.c
56 lines (48 loc) · 1.24 KB
/
sol2.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
/**
* \file
* \brief [Problem 10](https://projecteuler.net/problem=10) solution
* \author [Krishna Vedala](https://github.com/kvedala)
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
/** Main function */
int main(int argc, char *argv[])
{
long n = 100;
long long sum = 0;
char *sieve = NULL;
if (argc == 2) /* if command line argument is provided */
n = atol(argv[1]); /* use that as the upper limit */
/* allocate memory for the sieve */
sieve = calloc(n, sizeof(*sieve));
if (!sieve)
{
perror("Unable to allocate memory!");
return -1;
}
/* build sieve of Eratosthenes
In the array,
* if i^th cell is '1', then 'i' is composite
* if i^th cell is '0', then 'i' is prime
*/
for (long i = 2; i < sqrtl(n); i++)
{
/* if i^th element is prime, mark all its multiples
as composites */
if (!sieve[i])
{
for (long j = i * i; j < n + 1; j += i)
{
sieve[j] = 1;
}
sum += i;
}
}
for (long i = sqrtl(n) + 1; i < n; i++)
if (!sieve[i])
sum += i;
free(sieve);
printf("%ld: %lld\n", n, sum);
return 0;
}