This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question on Palindromes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on Palindromes" Watch "Question on Palindromes" New topic
Author

Question on Palindromes

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm currently trying to solve a problem that is on project Euler (projecteuler.net) that deals with finding the largest palindrome made from the product of two 3-digit numbers. I think the code I have written works, but the problem I'm running into is:
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
I'm exceeding the memory limit. I was just wondering if this was because im using Integers.

Here's my code:
Class 1:


Class 2:


Anything anyone can think of would be helpful. Thanks.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

No, it's because you are trying to create an array which is way too big for your computer's memory. Instead of doing this, you could take this comment:

and work on that. Hint: what's the largest possible product of two three-digit numbers?

Or better still you could produce a solution which doesn't require an array at all.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The largest possible product of two 3-digit numbers would be:

999 * 999 = 998001

I'm not really sure how i'd implement this without an array or some similar data structure, because don't i need to store all the palindromes i find so i can get the largest one??

Hunter.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
How about this?

1) have a variable BiggestPalin = 0;
2) use the same iteration that you have used to create the array.
3) for each iteration, calculate the product, check if its a palindrome.
4) if the product is a palindrome, check if its greater than BiggestPalin, If yes, update BiggestPalin.
5) Print the value finally.

This would not need an array or other data Structures.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
You would only need 1 variable to hold the largest number. If the new palindrome is larger you can replace the number in the variable.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
<>
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Thanks for your help everyone I finally got it.

Hunter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Palindromes