Question on Palindromes

Hunter McMillen
Ranch Hand
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.

Paul Clapham
Sheriff
Posts: 21443
33
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
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
Posts: 317

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
Posts: 230
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
Posts: 317
<>

Hunter McMillen
Ranch Hand
Posts: 492
Thanks for your help everyone I finally got it.

Hunter.