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.

> 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)

I suspect the error is in the line above.

>     end
>   end
>   puts "*** #{val} ***"
> end
> 
> smallest_divisible(20)

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end