C Implementation of Problem 10
View source code here on GitHub!
Includes
"macros.h" (implicitly, via primes.h)
"math.h" (implicitly, via primes.h & if compiled on PCC)
<stdlib.h>
(implicitly, via primes.h & if not compiled on PCC)<math.h>
(implicitly, via primes.h & if not compiled on PCC)<stdbool.h>
(implicitly, via primes.h)
Solution
-
uint64_t p0010()
-
int main(int argc, char const *argv[])
Note
This function is only present in the Python test runner, or when compiling as a standalone program. It is not present when compiling for the Unity test runner.
1/*
2Project Euler Problem 10
3
4This is where my prime infrastructure shows its weak points, since the C version doesn't have a sieve.
5
6Problem:
7
8The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
9
10Find the sum of all the primes below two million.
11*/
12#ifndef EULER_P0010
13#define EULER_P0010
14#include <stdint.h>
15#include <inttypes.h>
16#include <stdio.h>
17#include "include/macros.h"
18#include "include/primes.h"
19
20uint64_t EMSCRIPTEN_KEEPALIVE p0010() {
21 uint64_t tmp, answer = 0;
22 prime_sieve ps = prime_sieve0();
23 while ((tmp = next(ps)) < 2000000)
24 answer += tmp;
25 free_prime_sieve(ps);
26 return answer;
27}
28
29PROGRAM_TAIL("%" PRIu64, p0010)
30#endif