------ art_19006_8040958.1176755811091 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline The inject version should work for any value, as it is iterative. The recursive version probably breaks somewhere in the thousands (on my machine just under 2000), but could be machine dependent. --Tyler Prete On 4/16/07, Jason Roelofs <jameskilton / gmail.com> wrote: > > On 4/16/07, James Edward Gray II <james / grayproductions.net> wrote: > > > > On Apr 16, 2007, at 1:58 PM, David Simas wrote: > > > > > On Tue, Apr 17, 2007 at 02:56:16AM +0900, Jason Roelofs wrote: > > >> No and most likely not. > > >> > > >> def fact(n) > > >> if n 0 > > >> 1 > > >> else > > >> n * fact(n-1) > > >> end > > >> end > > > > > > For large enough n, this will overflow the stack. Since Ruby doesn't > > > optimize tail-recursive functions (and the above isn't tail recursive, > > > anyway), you'd better write this function as a loop (left as an > > > exercise). > > > > >> class Integer > > >> def fact > > >> (2..self).inject(1) { |f, n| f * n } > > >> end > > >> end > >