Project Euler/17
From charlesreid1
Contents
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
Project Euler project euler notes
Round 1: Problems 1-20 Problem 1 · Problem 2 · Problem 3 · Problem 4 · Problem 5 · Problem 6 · Problem 7 · Problem 8 · Problem 9 · Problem 10 Problem 11 · Problem 12 · Problem 13 · Problem 14 · Problem 15 · Problem 16 · Problem 17 · Problem 18 · Problem 19 · Problem 20 Round 2: Problems 50-70 Problem 51 · Problem 52 · Problem 53 · Problem 54 · Problem 55 · Problem 56 · Problem 57 · Problem 58 · ... · Problem 61 · Problem 62 · Problem 63 · Problem 64 · Problem 65 · Problem 66 · Problem 67 Round 3: Problems 100-110 Problem 100 · Problem 101 · Problem 102 Round 4: Problems 500-510 Problem 500 · Problem 501 * · Problem 502 * Round 5: Problems 150-160 Round 6: Problems 250-260 Round 7: Problems 170-180
|