C Implementation of Problem 34

View source code here on GitHub!

Includes

Solution

uint64_t p0034()
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 34
 3
 4Problem:
 5
 6145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
 7
 8Find the sum of all numbers which are equal to the sum of the factorial of
 9their digits.
10
11Note: as 1! = 1 and 2! = 2 are not sums they are not included.
12*/
13#ifndef EULER_P0034
14#define EULER_P0034
15#include <stdint.h>
16#include <inttypes.h>
17#include <stdio.h>
18#include "include/macros.h"
19#include "include/digits.h"
20#include "include/math.h"
21
22uint64_t EMSCRIPTEN_KEEPALIVE p0034() {
23    uint64_t answer = 0, sum;
24    digit_counter dc;
25    for (uint64_t i = 10; i < 100000; i++) {
26        sum = 0;
27        dc = digits(i);
28        while (!dc.exhausted)
29            sum += factorial(next(dc));
30        if (sum == i)
31            answer += i;
32        free_digit_counter(dc);
33    }
34    return answer;
35}
36
37PROGRAM_TAIL("%" PRIu64, p0034)
38#endif

Tags: factorial, digit-sum, c-iterator