It’s always good to see that the code you write has some real-life application, however small. So, it was heartening to find a Project Euler problem that required exactly the functionality provided by my Num2Str utility.
Project Euler
In an attempt to hone my Haskell skills, I have been playing around with Project Euler. Combining as it does programming with mathematics it is the perfect playground for me. The programs required are usually relatively short in both length and running time and so are ideal when you have a few minutes spare.
Validation!
Upon reaching the 17th challenge, I was delighted to be confronted by the 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.
Immediately reaching for my trusty num2str function, I found that my algorithm agreed with the results given for 1,2,3,4,5 and 342. Validation is a great thing and in this case gave me a sense of enormous well-being. Admittedly it did require a minor post-processing step to remove spaces and hyphens, but that was to be expected and easy in Haskell (filter isLetter anyone?).
It was but a small step from validation to merrily running the full problem and getting the correct answer.
Small victories
It is small victories such as this that make life worth living. I am now on a quest to become the highest rated Scottish Haskeller (login required) on Project Euler. I have some way to go but am currently in 7th place. Onwards for Freedom!
No related posts.
