C# Implementation of Problem 17

View source code here on GitHub!

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

Tags: word-problem, combinatorics