Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Project Euler #17

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"fourty" is misspelled, it should be "forty".

Regards,
Uli
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Uli, I cant believe I missed that.
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;)

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with the one above?
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing wrong with your approach, i guess Campbell wanted to challenge my ambition to create my own solution.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uli Hofstoetter wrote:i guess Campbell wanted to challenge my ambition to create my own solution.
Exactly
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what was your ruby solution for problem 18 Jesper?

Here's my solution in Ruby:


Ruby is awesome
Hunter.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can tell you now that that solution, although elegant, will not scale to Problem 67
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha, no don't post it, ill figure it out then post mine in the thread.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it



Solves in a few seconds
Hunter.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my Ruby implementation:

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic