# Numbers to letters

Published on 2011-07-25The seventeenth installment of Project Euler was a bit of a slog. Ignoring spacing, hyphens, and Americanisms, the Project Euler novitiate is asked to add the number of letters in all numbers from one to a thousand:

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

The solution was not difficult but took a long time to write. A smorgasbord of switch statements can be found below.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Problem017 { class Program { static void Main(string[] args) { int letterSum = 11; // "one thousand" int ones; int tens; int hundreds; for (int i = 1; i < 1000; i++) { ones = i % 10; tens = (i % 100) / 10; hundreds = i / 100; if (tens == 1) { switch (ones) { case 0: letterSum += 3; break; case 1: case 2: letterSum += 6; break; case 3: case 4: case 8: case 9: letterSum += 8; break; case 5: case 6: letterSum += 7; break; case 7: letterSum += 9; break; } } else { switch (ones) { case 1: case 2: case 6: letterSum += 3; break; case 3: case 7: case 8: letterSum += 5; break; case 4: case 5: case 9: letterSum += 4; break; } } if (tens > 1) { switch (tens) { case 2: case 3: case 8: case 9: letterSum += 6; break; case 4: case 5: case 6: letterSum += 5; break; case 7: letterSum += 7; break; } } if (hundreds > 0) { if (i % 100 != 0) letterSum += 10; // "hundred and" else letterSum += 7; // "hundred" switch (hundreds) { case 1: case 2: case 6: letterSum += 3; break; case 3: case 7: case 8: letterSum += 5; break; case 4: case 5: case 9: letterSum += 4; break; } } } Console.WriteLine("The number of letters used would be {0}", letterSum); } } }