From: Alan Malloy Date: 2012-02-18T01:41:07+09:00 Subject: Re: Factorial --e89a8ff250ee69cbd604b92b9cc7 Content-Type: text/plain; charset=ISO-8859-1 This method isn't as fast as using Math.gamma, but I believe any reasonable factorial can be calculated. A factorial of 10,000 benchmarked at 0.121007 seconds using this approach, where 100,000 took 14.354879 seconds. (1..n).inject(:*) On Fri, Feb 17, 2012 at 8:55 AM, Tanaka Akira wrote: > 2012/2/17 Junayeed Ahnaf Nirjhor : > > > > What is the most efficient way in Ruby to calculate the factorial of any > > given sum? Is there any way to make it as fast as in C? > > > > Complexity doesn't matter as long as it works correctly. > > How about Math.gamma(n+1).to_i for fact(n) assuming n <= 22 ? > > If the restriction, n <= 22 is not acceptable, > table lookup is another option. > > I think C is not so useful here because fact(21) overflows 64bit signed > int. > -- > Tanaka Akira > > --e89a8ff250ee69cbd604b92b9cc7--