From: Jacob Fugal Date: 2005-12-24T04:51:17+09:00 Subject: Re: Why not Python? (No, no, I am not a spy) On 12/23/05, Jeff Wood wrote: > # complete recursive: > > def factorial( x ) > return 0 if x < 1 > x * factorial( x - 1 ) > end > > # tail-recursive: > > def factorial( x, sum = 1 ) > return 0 if x < 1 > factorial( ( x - 1 ), ( sum * x ) ) > end > > # iterative: > > def factorial( x ) > sum = 1 > for i in (1..x).to_a > sum *= i > end > end With bugfixes: # complete recursive: def factorial( x ) return 1 if x < 1 x * factorial( x - 1 ) end # tail-recursive: def factorial( x, sum = 1 ) return sum if x < 1 factorial( ( x - 1 ), ( sum * x ) ) end # iterative: def factorial( x ) sum = 1 for i in (1..x).to_a sum *= i end sum end :) Jacob Fugal