programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Liutauras Vilda
• Knute Snortum
• Bear Bibeault
Sheriffs:
• Devaka Cooray
• Jeanne Boyarsky
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Carey Brown
• salvin francis
Bartenders:
• Tim Holloway
• Piet Souris
• Frits Walraven

Project Euler #17

Ranch Hand
Posts: 492
Hey everyone,

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.
Thanks,
Hunter.

Ranch Hand
Posts: 57
"fourty" is misspelled, it should be "forty".

Regards,
Uli

Ranch Hand
Posts: 128
Wow, Uli, that's a keen eye you have! I've read the code twice and thought to myself "everything seems fine" Didn't think to check the spelling

Hunter McMillen
Ranch Hand
Posts: 492
Thank you very much Uli, I cant believe I missed that.

Uli Hofstoetter
Ranch Hand
Posts: 57
You're welcome.

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 ;)

Marshal
Posts: 68061
258
• 1
Project Euler is by no means easy. It requires no slight mathematical ability. And you will have to produce a completely different solution for no 17

Hunter McMillen
Ranch Hand
Posts: 492
What's wrong with the one above?

Uli Hofstoetter
Ranch Hand
Posts: 57
Nothing wrong with your approach, i guess Campbell wanted to challenge my ambition to create my own solution.

Java Cowboy
Posts: 16084
88
• 1
Project Euler is fun if you like mathematics and programming. So far I have solved problems 1 to 18 and 67. For most of them, I wrote small programs in Ruby instead of Java. My Ruby solution to #17 is pretty similar to your Java solution. (I don't have it here, so I can't post my Ruby program right now...).

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.

Campbell Ritchie
Marshal
Posts: 68061
258

Uli Hofstoetter wrote:i guess Campbell wanted to challenge my ambition to create my own solution.

Exactly

Hunter McMillen
Ranch Hand
Posts: 492
what was your ruby solution for problem 18 Jesper?

Here's my solution in Ruby:

Ruby is awesome
Hunter.

Ranch Hand
Posts: 1296
I can tell you now that that solution, although elegant, will not scale to Problem 67

Jesper de Jong
Java Cowboy
Posts: 16084
88
My solution to problem 18 will scale, and also solves problem 67 in a fraction of a second.

Do you want me to post it here? It will spoil the fun of finding it out for yourself...

Hunter McMillen
Ranch Hand
Posts: 492
haha, no don't post it, ill figure it out then post mine in the thread.

Hunter McMillen
Ranch Hand
Posts: 492
Got it

Solves in a few seconds
Hunter.

Ranch Hand
Posts: 266

Hunter McMillen wrote:Got it

...

Solves in a few seconds
Hunter.

Well done Hunter! Smart to do this "bottoms up".
; )

Jesper de Jong
Java Cowboy
Posts: 16084
88
Here's my Ruby implementation:

Garrett Rowe
Ranch Hand
Posts: 1296
And just join in the multi-language parae here, here's my solution in Haskell

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.

Garrett Rowe
Ranch Hand
Posts: 1296
[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]

Hunter McMillen
Ranch Hand
Posts: 492
I like yours better than mine Jesper, it uses Ruby functions to solve, mine uses a function i defined and a loop structure found in most languages, I'm new to ruby, I didn't know it had a max function.

Hunter.

Jesper de Jong
Java Cowboy
Posts: 16084
88
You didn't know that Ruby had a max function (it's a method of Ruby's class Array)? But you're using it yourself in line 23 of your post above!

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:

author
Posts: 3281
8

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 .

Hunter McMillen
Ranch Hand
Posts: 492
Well, I have a selective memory I suppose. Thanks for the tip about "monkey patching" i didn't know that. The inject method is also a really simplified way to do what java does in like 10 lines so thats cool, I actually just ordered a beginners Ruby textbook, so I should be learning a lot more soon.

Hunter.

 What's a year in metric? Do you know this metric stuff tiny ad? Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java