C Implementation of Problem 17

View source code here on GitHub!

Includes

Solution

uint64_t p0017()
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 17
 3
 4Realizing I don't need the actual string made it much easier to write a C version
 5
 6Problem:
 7
 8If the numbers 1 to 5 are written out in words: one, two, three, four, five,
 9then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
10
11If all the numbers from 1 to 1000 (one thousand) inclusive were written out in
12words, how many letters would be used?
13
14NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
15forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
16letters. The use of "and" when writing out numbers is in compliance with
17British usage.
18*/
19#ifndef EULER_P0017
20#define EULER_P0017
21#include <stdint.h>
22#include <inttypes.h>
23#include <stdio.h>
24#include "include/macros.h"
25
26uint32_t to_string_len(uint64_t n);
27uint32_t to_string_len(uint64_t n) {
28    if (n >= 1000)
29        return to_string_len(n / 1000 % 100) + 8;  //len("thousand")
30    else if (n >= 100) {
31        uint32_t hundreds = to_string_len(n / 100 % 10) + 7;  // len("hundred")
32        if (n % 100)
33            return hundreds + 3 /* len("and") */ + to_string_len(n % 100);
34        return hundreds;
35    }
36    else if (n >= 20) {
37        uint32_t tens = 0;
38        switch (n / 10) {
39            case 4:
40                tens = 5;  // len("forty")
41                break;
42            case 5:
43                tens = 5;  // len("fifty")
44                break;
45            case 6:
46                tens = 5;  // len("sixty")
47                break;
48            case 7:
49                tens = 7;  // len("seventy")
50                break;
51            default:
52                tens = 6;  // len("ninety") and the like
53                break;
54        }
55        if (n % 10)
56            return tens + to_string_len(n % 10);
57        return tens;
58    }
59    switch (n) {
60        case 1: return 3;  // len("one")
61        case 2: return 3;  // len("two")
62        case 3: return 5;  // len("three")
63        case 4: return 4;  // len("four")
64        case 5: return 4;  // len("five")
65        case 6: return 3;  // len("six")
66        case 7: return 5;  // len("seven")
67        case 8: return 5;  // len("eight")
68        case 9: return 4;  // len("nine")
69        case 10: return 3;  // len("ten")
70        case 11: return 6;  // len("eleven")
71        case 12: return 6;  // len("twelve")
72        case 13: return 8;  // len("thirteen")
73        case 14: return 8;  // len("fourteen")
74        case 15: return 7;  // len("fifteen")
75        case 16: return 7;  // len("sixteen")
76        case 17: return 9;  // len("seventeen")
77        case 18: return 8;  // len("eighteen")
78        case 19: return 8;  // len("nineteen")
79        default: return -1;
80    }
81}
82
83uint64_t EMSCRIPTEN_KEEPALIVE p0017() {
84    uint64_t answer = 0;
85    for (uint32_t x = 1; x < 1001; x += 1)
86        answer += to_string_len(x);
87    return answer;
88}
89
90
91PROGRAM_TAIL("%" PRIu64, p0017)
92#endif

Tags: word-problem, combinatorics