Recursion

Joshua Macy amused at webamused.com
Mon Jan 1 16:13:17 EST 2001


Bob Calco wrote:
> 
> Anyone:
> 
> I noticed the following while dabbling in Perl, Ruby and Python recently.
> Take your standard factorial implementation, a la:
> 
> PYTHON
> ======
> import sys
> 
> def factor(n):
>   if n == 1:
>     return 1
>   else:
>     return n*factor(n-1)
> 
> print factor(int(sys.argv[1]))
> 

 ... snip ...
> 
> The highest value for n you can run in Python is 12; after that you get
> overflow error messages. In Perl, the largest value is 170 (after which you
> get 1.#INF for an answer). In Ruby, you can input a value as high as 763
> before you get a SystemStackError (stack level too deep) error.
> 
> 

  The int() function is the culprit here.  Change it to long() and
Python can go up to 999L before getting a Maximum Recursion Depth
Exceeded error.  I think the 1000 depth may be a compile-time constant,
intended to keep runaway recursions from happening.


  Joshua



More information about the Python-list mailing list