Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Pascal triange...

by gumby (Scribe)
on Jun 19, 2002 at 09:10 UTC ( #175596=note: print w/replies, xml ) Need Help??

in reply to Pascal's triangle...

Using an approximation to Stirling's series:
use constant PI => 3.141592653589793238; ... sub factorial { my $n = shift; return (sqrt((2*$n + 1/3)*PI)*($n**$n)*exp(-$n)); }

Replies are listed 'Best First'.
Re^2: Pascal's triangle...
by particle (Vicar) on Jun 19, 2002 at 11:25 UTC
    use constant PI => 3.141592653589793238;
    since you're only calculating pi once, and inlining it, take advantage of the operating system's precision...

    use constant PI => 4 * atan 1, 1; ## or as i prefer # sub PI() { 4 * atan 1, 1 }

    ~Particle *accelerates*

      Yet another way:
      use Math::Complex; use constant PI => pi(); # or just pi();


      (the triplet paradiddle with high-hat)
      or in kansas...

      use constant PI => 3;

      ~Particle *accelerates*

        > or in kansas...

        > use constant PI => 3;

        Actually, in Kansas we think PI should be round,

        use constant PI => sprintf("%1.0f", 3.14159265358979);

        You're thinking of Oklahoma...


      use constant PI => 4 * atan2 1, 1;
      I can recall from memory more than 700 digits of pi ;-)

      BTW, that's the FPU's precision, not the OS's. (On machines that have an FPU, anyway.)

      /me is pedantic sometimes

      We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book

Re(2): Pascal triange...
by gumby (Scribe) on Jun 19, 2002 at 10:40 UTC
    Ok, it's been communicated that using the entropy function would provide a better approximation to nCr. So here is such a formula:

    Let H(eps) := -eps.log_2(eps)-(1-eps)log_2(1-eps) be a binary entropy function.

    Then binom(n, eps.n) is approximately equal to 2^(n.H(eps)).

    Where eps is short for epsilon.

    Update: Note that this uses the more common Stirling formula which is slightly less accurate as it truncates terms in the series (instead of approximating them like the formula i posted before).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://175596]
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2017-05-27 12:34 GMT
Find Nodes?
    Voting Booth?