[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: factorial in ruby

Gary Thomas

4/20/2007 7:12:00 PM

Except for calculating binomial coefficients for probability calculations or
closely related things. I can't think of any reason to calculate large
factorials. In the case of binomial coefficients it is better to cancel out
some of the factors and avoid calculating the huge factorials. If the
numbers are large, the use of approximations is almost certainly a better
approach

Cheers

Gary Thomas

> -----Original Message-----
> From: Amos King [mailto:amos.l.king@gmail.com]
> Sent: Thursday, 19 April 2007 3:35 a.m.
> To: ruby-talk ML
> Subject: Re: factorial in ruby
>
>
> make it non-recursive use
>
> class Integer
> def fact
> return 1 if self == 0
> (1..self).inject { |i,j| i*j }
> end
> end
>
> Then you can just call
> 120.fact and that will return the factorial of 120 with no overflow.
> You may want to add in a check for negatives
>
> On 4/17/07, Gary Thomas <gthomas@takaro.co.nz> wrote:
> > For large enough n it will also overflow the number. Probably the most
> > efficient way would be to use a loop up to some (not very
> large) number, to
> > use Stirling's approximation or its extension up to some larger
> number, and
> > throw an overfow exception for anything larger than that.
> >
> > Cheers
> >
> > Gary Thomas
> >
> > > -----Original Message-----
> > > From: David Simas [mailto:dsimas@imageworks.com]
> > > Sent: Tuesday, 17 April 2007 6:58 a.m.
> > > To: ruby-talk ML
> > > Subject: Re: factorial in ruby
> > >
> > >
> > > 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).
> > >
> > > DGS
> > >
> > > >
> > > > Jason
> > > >
> > > > On 4/16/07, Trans <transfire@gmail.com> wrote:
> > > > >
> > > > >Is factorial defined anywhere in Ruby's core or standard
> library. If
> > > > >not will it be in 1.9+?
> > > > >
> > > > >Thanks,
> > > > >T.
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
> >
> >
> >
> >
>
>
> --
> Amos King
> Ramped Media
> USPS
> Programmer/Analyst
> St. Louis, MO
> Looking for something to do? Visit ImThere.com
>
>



1 Answer

M. Edward (Ed) Borasky

4/21/2007 1:43:00 AM

0

Gary Thomas wrote:
> Except for calculating binomial coefficients for probability calculations or
> closely related things. I can't think of any reason to calculate large
> factorials. In the case of binomial coefficients it is better to cancel out
> some of the factors and avoid calculating the huge factorials. If the
> numbers are large, the use of approximations is almost certainly a better
> approach
>
A rule of thumb I was taught in the days when compute power was
expensive was that any factorial over 10! should be done using
Stirling's approximation. I think that's a reasonable strategy even today.

--
M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)
http://borasky-res...

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.