C Implementation of Problem 3
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
-
uint16_t p0003()
-
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 3
3
4More lazy functions this time. Took a little while to figure out how to
5eliminate the special case for 2.
6
7Problem:
8
9The prime factors of 13195 are 5, 7, 13 and 29.
10
11What is the largest prime factor of the number 600851475143 ?
12*/
13#ifndef EULER_P0003
14#define EULER_P0003
15#include <stdint.h>
16#include <inttypes.h>
17#include <stdio.h>
18#include "include/macros.h"
19#include "include/primes.h"
20
21uint16_t EMSCRIPTEN_KEEPALIVE p0003() {
22 uint16_t answer = 0;
23 prime_factor_counter pfc = prime_factors(600851475143);
24 while (!pfc.exhausted)
25 answer = next(pfc);
26 free_prime_factor_counter(pfc);
27 return answer;
28}
29
30PROGRAM_TAIL("%" PRIu16, p0003)
31#endif