C Implementation of Problem 30

View source code here on GitHub!

Includes

Solution

uint64_t p0030()
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 30
 3
 4Problem:
 5
 6Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
 7
 8    1634 = 1^4 + 6^4 + 3^4 + 4^4
 9    8208 = 8^4 + 2^4 + 0^4 + 8^4
10    9474 = 9^4 + 4^4 + 7^4 + 4^4
11
12As 1 = 1^4 is not a sum it is not included.
13
14The sum of these numbers is 1634 + 8208 + 9474 = 19316.
15
16Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
17*/
18#ifndef EULER_P0030
19#define EULER_P0030
20#include <stdint.h>
21#include <inttypes.h>
22#include <stdio.h>
23#include "include/macros.h"
24#include "include/digits.h"
25
26uint64_t EMSCRIPTEN_KEEPALIVE p0030() {
27    uint64_t answer = 0, sum, tmp;
28    for (uint64_t i = 2; i < 1000000; i++) {
29        digit_counter dc = digits(i);
30        sum = 0;
31        do {
32            tmp = next(dc);
33            sum += tmp * tmp * tmp * tmp * tmp;
34        } while (!dc.exhausted);
35        if (sum == i)
36            answer += i;
37        free_digit_counter(dc);
38    }
39    return answer;
40}
41
42PROGRAM_TAIL("%" PRIu64, p0030)
43#endif

Tags: power, digit-sum, c-iterator