Project Euler Problem 17
You are on the Home/Other Tutorials/Project Euler/Problem 17 page
Google
Web This Site

Project Euler - Problem 17

More about Project Euler.

Problem description

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.

Solution

I decide to do this the old fashioned way...by hand -- with Excel serving as the calculator.

Here's my reasoning.

Calculate the number of characters in the word corresponding to each of the numbers 1 through 9 -- (1)

Do the same kind of analysis for each of the numbers 10 through 19 -- (2), and

for each of the numbers 20, 30, ..., 90 -- (3)

For each group of hundreds (zero hundreds, one hundreds, two hundreds, etc.), there are 9 occurrences of (1) and 1 occurrence of (2).  There are also 10 occurrences of each of the words in (3).  Then, for each group of hundred starting with one hundred we can list the number of occurrences of the word hundred (100 occurrences) and the word and (99 occurrences).  Finally, don't forget the final 1000.  Now, just multiply the various lengths by their respective occurrences and add those results.