## Problem 17: Number letter counts

*Difficulty: Easy

Problem:

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.

How to solve:

This is what you need to solve this problem

numbers = {“”,“one”,“two”,“three”,“four”,“five”,“six”,“seven”,

“eight”,“nine”,“ten”};

dozens =  {“”,“”,“twenty”,“thirty”,“forty”,“fifty”,“sixty”,

“seventy”, “eighty”, “ninety”};

lessThanTwenty = {“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“eleven”,“twelve”,“thirteen”,“fourteen”,“fifteen”,“sixteen”,“seventeen”,“eighteen”,“nineteen”};

1 = numbers[1];

10 = numbers[10]

11 = lessThanTwenty[11];

20 = dozens[20/10]

21 = dozens[20/10]+numbers[20%10]

….

You got the idea

Or you could analyze the problem like this

Project Euler 17: Letters in the numbers 1-1000

Solution took 36 ms

Implementation: Java

public class Prob17 {

private static final String[] numbers = {“”,“one”,“two”,“three”,“four”,“five”,“six”,“seven”,

“eight”,“nine”,“ten”};

private static final String[] dozens =  {“”,“”,“twenty”,“thirty”,“forty”,“fifty”,“sixty”,

“seventy”, “eighty”, “ninety”};

privatestaticfinalString[] lessThanTwenty = {

“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,

“eleven”,“twelve”, “thirteen”,“fourteen”,

“fifteen”,“sixteen”,“seventeen”,“eighteen”,“nineteen”};

private static String returnNumberInWords(int n) {

Strings = String.valueOf(n);

StringBuilderresult = new StringBuilder();

if(s.length() == 4)

result.append(“onethousand”);

else{

switch(s.length()) {

case1:

result.append(numbers[n]);

break;

case2:

if(n==10) result.append(numbers[n]);

else if( n < 20) {

result.append(lessThanTwenty[n]);

} else {

result.append(dozens[Integer.valueOf(s.charAt(0)+“”)]);

result.append((n%10!=0 ? numbers[Integer.valueOf(s.charAt(1)+“”)] : “”));

}

break;

case3:

//if(s.charAt(0) == ‘1’)

//result.append(“ahundred“);

//else

result.append(numbers[Integer.valueOf(s.charAt(0)+“”)] + “hundred”);

if(n%100!=0) {

String dozenS = s.substring(1);

int dozenN = Integer.valueOf(dozenS);

result.append(“and”);

if(dozenN <= 10)

result.append(numbers[dozenN]);

else if(dozenN < 20){

result.append(lessThanTwenty[dozenN]);

} else {

result.append(dozens[dozenN/10]);

}

if(dozenN > 20) {

int unit = Integer.valueOf(dozenS.charAt(1)+“”);

result.append(unit!=0 ? numbers[unit] : “”);

}

}

break;

}

}

System.out.println(result.length() + ” – “ + result.toString());

return result.toString();

}

publicstaticvoidmain(String[] args) {

int sum = 0;

int count1_9 = 0;

int count10_19 = 0;

int count20_99 = 0;

int count100_999 = 0;

for(int i = 1; i<= 1000; ++i)  {

int length =  returnNumberInWords(i).length();

sum+= length;

if(i >= 1 && i < 10) count1_9 += length;

else if(i >= 10 && i < 20) count10_19 += length;

else if(i >= 20 && i < 100) count20_99 += length;

else if(i >= 100 && i < 1000) count100_999 +=length;

}

System.out.println(sum);

System.out.println(“1-9: “ + count1_9

+“\n10-19: ” + count10_19

+“\n20-99: ” + count20_99

+“\n100_999: ” + count100_999);

}

}