# SystemStackError

**URL:** <https://www.ruby-forum.com/t/systemstackerror/157004>\
**Category:** Ruby\
**Created:** [January 6, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/systemstackerror/157004 "2009-01-06T12:04:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [January 6, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/systemstackerror/157004/1 "2009-01-06T12:04:53Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![marksbrain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@marksbrain](https://www.ruby-forum.com/u/marksbrain)\
**Post date:** [January 6, 2009, 12:34pm UTC](https://www.ruby-forum.com/t/systemstackerror/157004/2 "2009-01-06T12:34:17Z")

</div>

On 06.01.2009 11:40, Mark W. 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
