Hi all, What is a SystemStackError? Regarding code below: If I run it with limit=10 and smallest_divisible(10), I get 2520 (right answer), but it borks at 20. In the output I do get some numbers that are divisible from 1 to 11, but no 12 (or higher). Could an output of 12 or higher be causing the SystemStackError? If so is there some way to improve the code to eliminate the SystemStackError? Funny how 23480 throws the error, but then further numbers are processed. Then it stops completely at 24660! --- code ------------------------------------------------------------- #!/usr/bin/env ruby =begin Project Euler - problem #5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? =end def smallest_divisible(val) limit = 20 puts val (1..limit).each do |x| print "x is #{x}, " if val%x != 0 puts "" smallest_divisible(val + limit) end end puts "*** #{val} ***" end smallest_divisible(20) --- part of the output --------------------------------------- 23280 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, 23300 x is 1, x is 2, x is 3, 23320 x is 1, x is 2, x is 3, 23340 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, 23360 x is 1, x is 2, x is 3, 23380 x is 1, x is 2, x is 3, 23400 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, 23420 x is 1, x is 2, x is 3, 23440 x is 1, x is 2, x is 3, 23460 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, 23480 x is euler/05/p5.rb:14: in `smallest_divisible': stack level too deep (SystemStackError) from euler/05/p5.rb:18:in `smallest_divisible' from euler/05/p5.rb:14:in `each' from euler/05/p5.rb:14:in `smallest_divisible' from euler/05/p5.rb:18:in `smallest_divisible' from euler/05/p5.rb:14:in `each' from euler/05/p5.rb:14:in `smallest_divisible' from euler/05/p5.rb:18:in `smallest_divisible' from euler/05/p5.rb:14:in `each' ... 3685 levels... from euler/05/p5.rb:18:in `smallest_divisible' from euler/05/p5.rb:14:in `each' from euler/05/p5.rb:14:in `smallest_divisible' from euler/05/p5.rb:24 1, x is 2, x is 3, 23500 x is 1, x is 2, x is 3, 23520 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, x is 8, x is 9, 23540 x is 1, x is 2, x is 3, 23560 x is 1, x is 2, x is 3, 23580 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, 23600 x is 1, x is 2, x is 3, ... snipped ... 24580 x is 1, x is 2, x is 3, 24600 x is 1, x is 2, x is 3, x is 4, x is 5, x is 6, x is 7, 24620 x is 1, x is 2, x is 3, 24640 x is 1, x is 2, x is 3, 24660 cheers,

on 2009-01-06 12:04

on 2009-01-06 12:34

On 06.01.2009 11:40, Mark Woodward wrote: > What is a SystemStackError? robert@fussel ~ $ ruby -e 'def f;f;end;f' -e:1:in `f': stack level too deep (SystemStackError) from -e:1:in `f' from -e:1 As the error message says: your stack is too deep. You are simply hitting a limit of the interpreter. > Regarding code below: > If I run it with limit=10 and smallest_divisible(10), I get 2520 (right > answer), but it borks at 20. In the output I do get some numbers that > are divisible from 1 to 11, but no 12 (or higher). Could an output of 12 > or higher be causing the SystemStackError? If so is there some way to > improve the code to eliminate the SystemStackError? One way would be to rewrite to not use recursion. Before you do that you might want to look at your code again. It seems, you do not want to enter the recursion with "val + limit" but rather with "val + x". Otherwise the iteration would not really make sense - especially since "limit" is a constant. If you want to see what happens you can add "puts val" at the beginning of the function. > > puts "" > smallest_divisible(val + limit) I suspect the error is in the line above. > end > end > puts "*** #{val} ***" > end > > smallest_divisible(20) Kind regards robert