From: Jason Roelofs Date: 2009-10-01T04:55:38+09:00 Subject: Re: SystemStackError: stack level too deep > how make it deeper? --001517740442f26db70474d0e954 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Sep 30, 2009 at 3:47 PM, Joshua Muheim wrote: > Hi all > > I have a recursive algorith that must run until it finds the result: > > def p(n) > n * (3 * n - 1) / 2 > end > > def h(n) > n * (2 * n - 1) > end > > def find_next_match(p, h) > result_p = p(p + 1) > result_h = h(h + 1) > if result_p == result_h # Resultat gefunden! > return result_p > else > result_p < result_h ? p += 1 : h += 1 > > puts "find next match for p=#{p} (#{result_p}) and h=#{h} > (#{result_h})" > find_next_match(p, h) > end > end > > The task is to find the next pair of p and h that result in the same > number. The first pair is 165 and 143, so I run it with these arguments: > > puts find_next_match(165, 143) > > Sadly, after some seconds, Ruby throwns a SystemStackError: > > SystemStackError: stack level too deep > method p in uebung-2-1.rb at line 2 > method find_next_match in uebung-2-1.rb at line 10 > method find_next_match in uebung-2-1.rb at line 18 > at top level in uebung-2-1.rb at line 22 > copy output > Program exited with code #1 after 8.29 seconds. > > But I'd like to run it and run it and run it without this limitation. Is > there a workaround for this, so I can change the stack level maximum? > > Thanks a lot for help > Josh > -- > Posted via http://www.ruby-forum.com/. > > Rewrite it to not be recursive. Jason --001517740442f26db70474d0e954--