Rust Implementation of Problem 17

View source code here on GitHub!

Problem Solution

pub fn problems::p0017::p0017() -> utils::Answer
 1/*
 2Project Euler Problem 17
 3
 4I feel like there is a better way to recurse this problem, but I could not
 5think of one at the time
 6
 7Problem:
 8
 9If the numbers 1 to 5 are written out in words: one, two, three, four, five,
10then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
11
12If all the numbers from 1 to 1000 (one thousand) inclusive were written out in
13words, how many letters would be used?
14
15NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
16forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
17letters. The use of "and" when writing out numbers is in compliance with
18British usage.
19*/
20use crate::include::utils::Answer;
21
22fn to_string(n: u16) -> String {
23    if n >= 1000 {
24        let mut thousands = to_string(n / 1000 % 100);
25        thousands.push_str(" thousand");
26        if n % 1000 != 0 {
27            thousands.push(' ');
28            thousands.push_str(&to_string(n % 1000));
29        }
30        return thousands;
31    }
32    if n >= 100 {
33        let mut hundreds = to_string(n / 100 % 10);
34        hundreds.push_str(" hundred");
35        if n % 100 != 0 {
36            hundreds.push_str(" and ");
37            hundreds.push_str(&to_string(n % 100));
38        }
39        return hundreds;
40    }
41    if n >= 20 {
42        let mut tens = match n / 10 {
43            2 => "twenty",
44            3 => "thirty",
45            4 => "forty",
46            5 => "fifty",
47            6 => "sixty",
48            7 => "seventy",
49            8 => "eighty",
50            9 => "ninety",
51            _ => unreachable!()
52        }.to_owned();
53        if n % 10 != 0 {
54            tens.push('-');
55            tens.push_str(&to_string(n % 10));
56        }
57        return tens;
58    }
59    if n > 12 {
60        let mut prefix = match n {
61            13 => "thir",
62            14 => "four",
63            15 => "fif",
64            16 => "six",
65            17 => "seven",
66            18 => "eigh",
67            19 => "nine",
68            _ => unreachable!()
69        }.to_owned();
70        prefix.push_str("teen");
71        return prefix;
72    }
73    return match n {
74        0 => "zero",
75        1 => "one",
76        2 => "two",
77        3 => "three",
78        4 => "four",
79        5 => "five",
80        6 => "six",
81        7 => "seven",
82        8 => "eight",
83        9 => "nine",
84        10 => "ten",
85        11 => "eleven",
86        12 => "twelve",
87        _ => unreachable!()
88    }.to_owned();
89}
90
91
92pub fn p0017() -> Answer {
93    let mut answer: u16 = 0;
94    for x in 1..=1000 {
95        let string = to_string(x);
96        answer += string.replace(" ", "").replace("-", "").len() as u16;
97    }
98    return Answer::Int(answer.into());
99}

Tags: word-problem, combinatorics