Project Euler: Problem 17 – Number letter counts

Problem 17:

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?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.

Idea:

I initially wanted to give an answer less than 26, since there’s no more than 26 letters in the English alphabet (sadly, the answer was wrong).

Afterwards, I tried just counting the number of letters in each “group”: ones, ‘teens’ (because 11-15 don’t follow the convention of the rest of the tens group), tens, and hundreds; then figured out where each number fell in terms of groups to get the sum of the letters. However, my answer was still incorrect.

Finally, I decided to just convert the numbers into actual words (without hyphens or spaces) to ensure I was forming and counting correctly. Once I saw everything was correct, I just added all the lengths together.

```int answer = 0;

for(int i = 1; i < = 1000; i++) {
try {
} catch(IOException e) {e.printStackTrace();}
}

//================================
private static int countLetters(int number) throws IOException {
String word = "";
if(number == 1000) {
word = "OneThousand";
return word.length();
}
int hundreds = number/100;
switch(hundreds) {
case 1: word+="OneHundred"; break;
case 2: word+="TwoHundred"; break;
case 3: word+="ThreeHundred"; break;
case 4: word+="FourHundred"; break;
case 5: word+="FiveHundred"; break;
case 6: word+="SixHundred"; break;
case 7: word+="SevenHundred"; break;
case 8: word+="EightHundred"; break;
case 9: word+="NineHundred"; break;
}

if(number % 100 != 0 && number > 100) {
word+="and";
}

int tens = (number % 100) / 10;
switch(tens) {
case 1:
int value = number%100;
switch(value){
case 19: word+="Nineteen"; break;
case 18: word+="Eighteen"; break;
case 17: word+="Seventeen"; break;
case 16: word+="Sixteen"; break;
case 15: word+="Fifteen"; break;
case 14: word+="Fourteen"; break;
case 13: word+="Thirteen"; break;
case 12: word+="Twelve"; break;
case 11: word+="Eleven"; break;
case 10: word+="Ten"; break;
}
break;
case 2: word+="Twenty"; break;
case 3: word+="Thirty"; break;
case 4: word+="Forty"; break;
case 5: word+="Fifty"; break;
case 6: word+="Sixty"; break;
case 7: word+="Seventy"; break;
case 8: word+="Eighty"; break;
case 9: word+="Ninety"; break;
}

if(number%100 >= 10 && number%100 <= 19) {
return word.length();
} else {
int ones = number % 10;
switch(ones) {
case 1: word+="One"; break;
case 2: word+="Two"; break;
case 3: word+="Three"; break;
case 4: word+="Four"; break;
case 5: word+="Five"; break;
case 6: word+="Six"; break;
case 7: word+="Seven"; break;
case 8: word+="Eight"; break;
case 9: word+="Nine"; break;
}
}
return word.length();
}```