aspose file tools*
The moose likes Java in General and the fly likes Project Euler #17 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Project Euler #17" Watch "Project Euler #17" New topic
Author

Project Euler #17

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
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.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
"fourty" is misspelled, it should be "forty".

Regards,
Uli


SCEA5, Certified ScrumMaster
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
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

Joined: Mar 13, 2009
Posts: 492

Thank you very much Uli, I cant believe I missed that.
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
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 ;)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Mar 13, 2009
Posts: 492

What's wrong with the one above?
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Uli Hofstoetter wrote:i guess Campbell wanted to challenge my ambition to create my own solution.
Exactly
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

what was your ruby solution for problem 18 Jesper?

Here's my solution in Ruby:


Ruby is awesome
Hunter.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I can tell you now that that solution, although elegant, will not scale to Problem 67


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Mar 13, 2009
Posts: 492

Got it



Solves in a few seconds
Hunter.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Here's my Ruby implementation:

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
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

Joined: Jan 17, 2006
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

Joined: Mar 13, 2009
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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

Joined: Jun 24, 2003
Posts: 3274
    
    5

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 .


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Project Euler #17