Problem 17 on project euler is this:
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?
I've written the program and it works for 1 to 5, but when I use 1-1000 im getting an extra 100 letters in my result and im not really sure why.
Here's the code:
the result I get is 21224, the actual answer is 21124
Any help would be great.
But i am not sure, if i should thank you or hate you for pointing me to Euler project, which i did not know before ... i guess it will cost me plenty of time and nerves in the near future ;)
Problem #17 is one of the more straightforward (easy) problems, some of the other problems are much harder and require you to think hours about the mathematics to find an efficient algorithm.
Uli Hofstoetter wrote:i guess Campbell wanted to challenge my ambition to create my own solution.
Here's my solution in Ruby:
Ruby is awesome
Do you want me to post it here? It will spoil the fun of finding it out for yourself...
Solves in a few seconds
Hunter McMillen wrote:Got it
Solves in a few seconds
Well done Hunter! Smart to do this "bottoms up".
I assure you it made perfect sense to me when I wrote it ~6 months ago, I haven't written a lot of Haskell since then so even I'm having a hard time understanding whats going on in this function. I know I used the bottom up approach, and that it gave me the right answer very quickly. I love writing Haskell but I really hate reading it... go figure.
You can do very nifty things with Ruby. For example, to add all elements in an array together, you can do this:
Or to multiply them, you can do this:
And you can even modify existing classes (this technique is called monkey patching), which allows you to add useful methods to class Enumerable, for example:
That will allow you to do things like this:
Garrett Rowe wrote:[epilogue] After looking at it for about 5 minutes, I get what I did here. It probably helps that I already knew the algorithm. I really should have used type annotations (optional in Haskell) to help me out. I take back what I said about Haskell, or at least amend it to read "I hate reading someone else's Haskell", with the caveat being that my own code can start looking like someone else's code in about 6 months.[/epilogue]
Just try reading C++ that's been converted from Haskell *shudder*, one of my uni profs was just insane .