• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Question on Palindromes  RSS feed

 
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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: 24464
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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 Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
<>
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help everyone I finally got it.

Hunter.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!