Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > SystemStackError

Reply
Thread Tools

SystemStackError

 
 
Mark Woodward
Guest
Posts: n/a
 
      01-06-2009
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,

--
Mark
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-06-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SystemStackError when rb_define_module_function() * 3 Suraj Kurapati Ruby 0 02-17-2006 06:20 AM
Lack of SystemStackError Daniel Berger Ruby 3 11-04-2005 05:50 AM
ruby-htmltools on cygwin - SystemStackError Lee Ruby 2 10-25-2004 07:07 PM
Stack level too deep (SystemStackError) Jesper Olsen Ruby 7 01-16-2004 12:19 PM
SystemStackError comparing self-referential structures Joel VanderWerf Ruby 2 08-22-2003 01:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57