Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question on Palindromes

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 27290
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<>
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help everyone I finally got it.

Hunter.
 
reply
    Bookmark Topic Watch Topic
  • New Topic