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,