From charlesreid1

The Problem

Problem 17 deals with the number of letters it takes to spell out a number.

https://projecteuler.net/problem=17

Solution

First, let it be said that this is a problem that shows how tremendously awful language rules can be, when contrasted with the happy certainty of mathematics.

This basically requires about 20 special cases, and from there things are relatively smooth. This program only goes up to 1,000, but it would be entirely plausible to extend this program to go even further and handle extremely large numbers in a more programmatic way.

Much like this problem, the larger the digits go, the more digits you can name with set of singular, systematic rules.

Consider, for example: naming the first 19 digits requires 19 separate rules. ("one", "two", "three", and so on through the teenagers.)

But once we reach twenty and twenty one, we have a 20th rule that introduces the word "twenty", and the twenty-first rule that covers the next 9 numbers. (Take "twenty" and add the spelling of any nonzero number in the ones place, which doesn't require any new rules).

Next, we get to thirty, and require a new label "thirty" but the same rule - add the spelling of any nonzero number in the ones place, thirty one, thirty two, thirty three, etc. Now, just a few rules can generalize and take us all the way to 99.

Once we get to the hundreds, we can name way more numbers - one additional rule (introducing the word "hundred") and following the pattern of adding "and" and the englishification of whatever comes after, takes us all the way to 999.

One more rule ("thousand") takes us to 999,999 new numbers. And now the system becomes more refined - all the special cases were all piled up at the very beginning.

Code

Link to code: https://git.charlesreid1.com/cs/euler/src/master/scratch/Round1_001-020/017

Flags