C Implementation of Problem 16

View source code here on GitHub!

Includes

Solution

uint64_t p0016()
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 16
 3
 4This was fairly easy to do, given the BCD infrastructure I'd built up already, but I feel like there's a better way
 5to do it than this, if I could manage arbitrary-precision multiplication more efficiently.
 6
 7Problem:
 8
 9215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
10
11What is the sum of the digits of the number 21000?
12*/
13#ifndef EULER_P0016
14#define EULER_P0016
15#include <stdint.h>
16#include <inttypes.h>
17#include <stdio.h>
18#include "include/macros.h"
19#include "include/bcd.h"
20
21uint64_t EMSCRIPTEN_KEEPALIVE p0016() {
22    uint64_t answer = 0;
23    BCD_int power = pow_cuint_cuint(256, 125);
24    for (size_t i = 0; i < power.bcd_digits; i++)
25        answer += (power.digits[i] & 0x0F) + (power.digits[i] >> 4);
26    return answer;
27}
28
29PROGRAM_TAIL("%" PRIu64, p0016)
30#endif

Tags: large-numbers, digit-sum, power