Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Pascal triange...

by gumby (Scribe)
on Jun 19, 2002 at 09:10 UTC ( [id://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();

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
      or in kansas...

      use constant PI => 3;
      ;-P

      ~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...

        YuckFoo

      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://175596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found