[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

SystemStackError

Mark Woodward

1/6/2009 10:40:00 AM

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

Robert Klemme

1/6/2009 11:16:00 AM

0

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

marcman

1/23/2011 2:18:00 PM

0

On Jan 22, 10:08 pm, volkfolk <volkfo...@verizon.net> wrote:
> On Jan 22, 6:51 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > On Jan 22, 4:51 pm, Neil X <nei...@yahoo.com> wrote:
>
> > > The Jets coaches and Jets players don't annoy me nearly as much as
> > > they do most Patriots fans, with one exception:  Ladainian Tomlinson.
> > > I really can't take the idea of this guy getting a ring.  If he
> > > weren't on the Jets, I'd probably root for NY over Pitt.  I'll never
> > > forget his press conference after the Pats beat SD in the 2006
> > > playoffs.  "You all know me, I'm a classy guy," is what he opened up
> > > his press conference with,
>
> > That's cool. He is a classy guy.
>
> No he isn't. He's a loudmouthed, egotistical prima donna.
>
> Scot
>
> PS. I didn't like him with the Chargers and I don't like him with the
> Jets

Well, he's no Curtis Martin, that's for sure!

J-E-T-S, JETS JETS JETS!

Neil X

1/23/2011 4:31:00 PM

0

On Jan 22, 6:51 pm, marcman <marcmanstud...@gmail.com> wrote:
> On Jan 22, 4:51 pm, Neil X <nei...@yahoo.com> wrote:
>
> > The Jets coaches and Jets players don't annoy me nearly as much as
> > they do most Patriots fans, with one exception:  Ladainian Tomlinson.
> > I really can't take the idea of this guy getting a ring.  If he
> > weren't on the Jets, I'd probably root for NY over Pitt.  I'll never
> > forget his press conference after the Pats beat SD in the 2006
> > playoffs.  "You all know me, I'm a classy guy," is what he opened up
> > his press conference with,
>
> That's cool. He is a classy guy.

Yeah, just ask him, he'll be glad to tell you. Real class, that.


> >  then proceeded to whine for 4 minutes about
> > the Pats showing a lack of respect by imitating Shawne Merriman's
> > "Lights Out" dance.  Yeah, the Pats have no class because they mocked
> > the "Lights Out" dance, a dance that a Charger player did to
> > disrespect the other team every time he sacked a QB.  No one on the
> > Pats has ever strutted around like a priapic peacock after tackling
> > someone.  
>
> No, just their All Pro MVP QB struts around pointing fingers at the
> opposing players bench practically every time he completes a pass.


Nice line, except that he didn't point any fingers after the first
Jets game, despite the claims by the second most annoying Jet,
Cromartie.


> Oh please, get over it already Neil, Pats are one and done. Come on
> back next year and we can pick this rivalry up again, in the meantime
> the Jets have an AFC Championship game to deal with . . .


It's not about the Pats, I never said it was.

Peace,
Neil X.

Neil X

1/23/2011 4:32:00 PM

0

On Jan 23, 9:18 am, marcman <marcmanstud...@gmail.com> wrote:
> On Jan 22, 10:08 pm, volkfolk <volkfo...@verizon.net> wrote:
>
>
>
>
>
>
>
>
>
> > On Jan 22, 6:51 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > > On Jan 22, 4:51 pm, Neil X <nei...@yahoo.com> wrote:
>
> > > > The Jets coaches and Jets players don't annoy me nearly as much as
> > > > they do most Patriots fans, with one exception:  Ladainian Tomlinson.
> > > > I really can't take the idea of this guy getting a ring.  If he
> > > > weren't on the Jets, I'd probably root for NY over Pitt.  I'll never
> > > > forget his press conference after the Pats beat SD in the 2006
> > > > playoffs.  "You all know me, I'm a classy guy," is what he opened up
> > > > his press conference with,
>
> > > That's cool. He is a classy guy.
>
> > No he isn't. He's a loudmouthed, egotistical prima donna.
>
> > Scot
>
> > PS. I didn't like him with the Chargers and I don't like him with the
> > Jets
>
> Well, he's no Curtis Martin, that's for sure!


That's for sure. There was a guy who really was all class.

Peace,
Neil X.

marcman

1/23/2011 7:38:00 PM

0

On Jan 23, 11:32 am, Neil X <nei...@yahoo.com> wrote:
> On Jan 23, 9:18 am, marcman <marcmanstud...@gmail.com> wrote:
>
>
>
>
>
> > On Jan 22, 10:08 pm, volkfolk <volkfo...@verizon.net> wrote:
>
> > > On Jan 22, 6:51 pm, marcman <marcmanstud...@gmail.com> wrote:
>
> > > > On Jan 22, 4:51 pm, Neil X <nei...@yahoo.com> wrote:
>
> > > > > The Jets coaches and Jets players don't annoy me nearly as much as
> > > > > they do most Patriots fans, with one exception:  Ladainian Tomlinson.
> > > > > I really can't take the idea of this guy getting a ring.  If he
> > > > > weren't on the Jets, I'd probably root for NY over Pitt.  I'll never
> > > > > forget his press conference after the Pats beat SD in the 2006
> > > > > playoffs.  "You all know me, I'm a classy guy," is what he opened up
> > > > > his press conference with,
>
> > > > That's cool. He is a classy guy.
>
> > > No he isn't. He's a loudmouthed, egotistical prima donna.
>
> > > Scot
>
> > > PS. I didn't like him with the Chargers and I don't like him with the
> > > Jets
>
> > Well, he's no Curtis Martin, that's for sure!
>
> That's for sure.  There was a guy who really was all class.
>
> Peace,
> Neil X.

With all kidding to the side, we absolutely agree on that.

marcman

1/23/2011 7:39:00 PM

0

On Jan 23, 11:31 am, Neil X <nei...@yahoo.com> wrote:

> It's not about the Pats

Sure thing, Bah-stan Bah-bby!

Good luck to the Bears today, I hope they win! :)

james.pablos

1/23/2011 11:33:00 PM

0

On Jan 23, 2:39 pm, marcman <marcmanstud...@gmail.com> wrote:

> Good luck to the Bears today, I hope they win! :)

They didn't.

Game time, baby!! You chewing your nails to the quick yet?