Results 1 to 4 of 4
  1. #1
    anupam.kumar is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default no heap space... need more heap

    hi guys ,

    i m kinda stuck with this ..

    this is a project euler problem ... the problem is this

    The following iterative sequence is defined for the set of positive integers:

    n → n/2 (n is even)
    n → 3n + 1 (n is odd)

    Using the rule above and starting with 13, we generate the following sequence:
    13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

    It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

    Which starting number, under one million, produces the longest chain?

    NOTE: Once the chain starts the terms are allowed to go above one million.
    i just went for straightforward bruteforce approach ...

    heres my code
    Java Code:
    public class pro14
    {
    	public static void main(String args[]) 
    	{
    		int j=0,pos=0;
                    int upperlimit=1000000;
    		ArrayList<Integer> temp = new ArrayList<Integer>();
    		ArrayList<Integer> longest = new ArrayList<Integer>();
    		for(int i=1;i<upperlimit;i++)
    	    {
    	        
    	        temp.add(new Integer(i));
    	        j=i;
    	        while(j!=1)
    	        {
    	            if(j%2==0)
    	            {
    	                j=j/2;       
                       }
                       else
                       {
                            j=3*j+1;
                       }
                    
                    temp.add(new Integer(j));
                   }
                if(longest.size() < temp.size())
                {
                    longest.clear();
                    for(int k=0;k<temp.size();k++)
                        longest.add(temp.get(k));                
                    pos=i;
                }            
                temp.clear();           
           }        
            System.out.println("The starting number that produces longest chain="+pos);    
            for(int i=0;i<longest.size();i++)
                System.out.print(longest.get(i)+"->");
                
        }
    
    	}
    the above code works for 100,1000,10000,100000 but not a million ... for example if i change upperlimit variable to 100000 (hundred thousand) i get the output as
    Java Code:
    The starting number that produces longest chain=77031
    77031->231094->115547->346642->173321->519964->259982->129991->389974->194987->584962->292481->877444->438722->219361->658084->329042->164521->493564->246782->123391->370174->185087->555262->277631->832894->416447->1249342->624671->1874014->937007->2811022->1405511->4216534->2108267->6324802->3162401->9487204->4743602->2371801->7115404->3557702->1778851->5336554->2668277->8004832->4002416->2001208->1000604->500302->250151->750454->375227->1125682->562841->1688524->844262->422131->1266394->633197->1899592->949796->474898->237449->712348->356174->178087->534262->267131->801394->400697->1202092->601046->300523->901570->450785->1352356->676178->338089->1014268->507134->253567->760702->380351->1141054->570527->1711582->855791->2567374->1283687->3851062->1925531->5776594->2888297->8664892->4332446->2166223->6498670->3249335->9748006->4874003->14622010->7311005->21933016->10966508->5483254->2741627->8224882->4112441->12337324->6168662->3084331->9252994->4626497->13879492->6939746->3469873->10409620->5204810->2602405->7807216->3903608->1951804->975902->487951->1463854->731927->2195782->1097891->3293674->1646837->4940512->2470256->1235128->617564->308782->154391->463174->231587->694762->347381->1042144->521072->260536->130268->65134->32567->97702->48851->146554->73277->219832->109916->54958->27479->82438->41219->123658->61829->185488->92744->46372->23186->11593->34780->17390->8695->26086->13043->39130->19565->58696->29348->14674->7337->22012->11006->5503->16510->8255->24766->12383->37150->18575->55726->27863->83590->41795->125386->62693->188080->94040->47020->23510->11755->35266->17633->52900->26450->13225->39676->19838->9919->29758->14879->44638->22319->66958->33479->100438->50219->150658->75329->225988->112994->56497->169492->84746->42373->127120->63560->31780->15890->7945->23836->11918->5959->17878->8939->26818->13409->40228->20114->10057->30172->15086->7543->22630->11315->33946->16973->50920->25460->12730->6365->19096->9548->4774->2387->7162->3581->10744->5372->2686->1343->4030->2015->6046->3023->9070->4535->13606->6803->20410->10205->30616->15308->7654->3827->11482->5741->17224->8612->4306->2153->6460->3230->1615->4846->2423->7270->3635->10906->5453->16360->8180->4090->2045->6136->3068->1534->767->2302->1151->3454->1727->5182->2591->7774->3887->11662->5831->17494->8747->26242->13121->39364->19682->9841->29524->14762->7381->22144->11072->5536->2768->1384->692->346->173->520->260->130->65->196->98->49->148->74->37->112->56->28->14->7->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1->
    so when i try the same for a million i get this

    Java Code:
    mintoo@mintoo pro14]$ java pro14
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    	at pro14.main(pro14.java:32)
    i have even tried

    Java Code:
    java -Xms512m -Xmx2048m pro14
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    	at java.util.Arrays.copyOf(Arrays.java:2772)
    	at java.util.Arrays.copyOf(Arrays.java:2746)
    	at java.util.ArrayList.ensureCapacity(ArrayList.java:187)
    	at java.util.ArrayList.add(ArrayList.java:378)
    	at pro14.main(pro14.java:32)
    [mintoo@mintoo pro14]$
    Last edited by anupam.kumar; 02-08-2010 at 12:19 PM.

  2. #2
    anupam.kumar is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default [solved]

    i did not need to consider the actual values themselves ... i just needed to consider the length ... i ll print the values to file if at all i need them ... but actually i dont

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by anupam.kumar View Post
    i did not need to consider the actual values themselves ... i just needed to consider the length ... i ll print the values to file if at all i need them ... but actually i dont
    So don't add them to a list; increment a simple counter instead.

    kind regards,

    Jos

  4. #4
    anupam.kumar is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Java heap space,
    By babyboban in forum Advanced Java
    Replies: 18
    Last Post: 01-14-2010, 12:22 PM
  2. Heap Space Problem
    By segolas in forum Advanced Java
    Replies: 6
    Last Post: 01-14-2010, 11:29 AM
  3. Java Heap Space
    By sandeeprao.techno in forum Advanced Java
    Replies: 19
    Last Post: 10-30-2008, 11:27 AM
  4. Java heap space error
    By gezzel in forum New To Java
    Replies: 19
    Last Post: 09-25-2008, 12:07 AM
  5. Java heap space?
    By javanewbie in forum New To Java
    Replies: 1
    Last Post: 06-24-2008, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •