Java Implementation of Problem 17

View source code here on GitHub!

public class p0017 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 17

  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*/
 20package euler;
 21
 22public class p0017 implements IEuler {
 23    @Override
 24    public Object answer() {
 25        int answer = 0;
 26        for (int x = 1; x < 1001; x += 1)
 27            answer += to_string(x).replace(" ", "")
 28                                  .replace("-", "")
 29                                  .length();
 30        return (short) answer;
 31    }
 32
 33    String to_string(int n) {
 34        if (n >= 1000)
 35            return to_string(n / 1000 % 100) + " thousand";
 36        else if (n >= 100) {
 37            String hundreds = to_string(n / 100 % 10) + " hundred";
 38            if (n % 100 != 0)
 39                return hundreds + " and " + to_string(n % 100);
 40            return hundreds;
 41        } else if (n >= 20) {
 42            String tens = "";
 43            switch (n / 10) {
 44            case 2:
 45                tens = "twenty";
 46                break;
 47            case 3:
 48                tens = "thirty";
 49                break;
 50            case 4:
 51                tens = "forty";
 52                break;
 53            case 5:
 54                tens = "fifty";
 55                break;
 56            case 6:
 57                tens = "sixty";
 58                break;
 59            case 7:
 60                tens = "seventy";
 61                break;
 62            case 8:
 63                tens = "eighty";
 64                break;
 65            case 9:
 66                tens = "ninety";
 67                break;
 68            }
 69            if (n % 10 != 0)
 70                return tens + "-" + to_string(n % 10);
 71            return tens;
 72        }
 73        switch (n) {
 74        case 1:
 75            return "one";
 76        case 2:
 77            return "two";
 78        case 3:
 79            return "three";
 80        case 4:
 81            return "four";
 82        case 5:
 83            return "five";
 84        case 6:
 85            return "six";
 86        case 7:
 87            return "seven";
 88        case 8:
 89            return "eight";
 90        case 9:
 91            return "nine";
 92        case 10:
 93            return "ten";
 94        case 11:
 95            return "eleven";
 96        case 12:
 97            return "twelve";
 98        case 13:
 99            return "thirteen";
100        case 14:
101            return "fourteen";
102        case 15:
103            return "fifteen";
104        case 16:
105            return "sixteen";
106        case 17:
107            return "seventeen";
108        case 18:
109            return "eighteen";
110        case 19:
111            return "nineteen";
112        }
113        return "";
114    }
115}

Tags: word-problem, combinatorics