Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
There was a more accurate formula than Stirling's in tilly's post. Wolfram's Mathematica attributes it to Gosper:
sub factorial_gosper { # sqrt(2*n*PI + 1/3)*n**n/exp(n) my $n = shift; return exp($n*log($n) - $n + 0.5*log(2*$n*PI + 1.0/3.0)); }

This gives better accuracy for small numbers. For 8!, I get 40034.48, compared to the correct 40320.

There are several problems with your implemenation of Stirling's formula. For one thing, log(e^(-n)), which you give as -e*log(n). Since the log is just the exponent base e, the correct answer is -n.

Tilly's formula is closer to being right; there was only one parenthesis mistake. I corrected that and tried them all in a modified version of your code:

#!/usr/bin/perl -w use strict; + my $h =8; my $pure = factorial_pure($h); my $stirling = factorial_stirling($h); my $real_stirling = real_stirling($h); my $gosper = factorial_gosper($h); + print "PURE : $pure\n"; print "STIRLING : $stirling\n"; print "REAL : $real_stirling\n"; print "GOSPER : $gosper\n"; + #------Subroutines------------ + sub factorial_stirling{ my $n = shift; use constant PI => 4*atan2 (1,1); use constant e => exp(1); + my $log_nfact = $n * log ($n) - e* log($n) + 0.5 * (log(2*PI)+log($n)); + # Below is Tilly's suggestion, with less accuracy # $n * log ($n) - ($n) + (0.5 * (log(2+ log(PI))+log($n))); + return exp($log_nfact); } + + sub factorial_pure { + my ($n,$res) = (shift,1); return undef unless $n>=0 and $n == int($n); $res *= $n-- while $n>1; return $res; + } + sub real_stirling { my $n = shift; my $log_nfact = $n*log($n) - $n + 0.5*(log(2) + log(PI)+ log($n)); return exp($log_nfact); } + sub factorial_gosper { # sqrt(2*n*PI + 1/3)*n**n/exp(n) my $n = shift; return exp($n*log($n) - $n + 0.5*log(2*$n*PI + 1.0/3.0)); } +
I get the answers below:
PURE : 40320 STIRLING : 417351.253768542 REAL : 39902.3954526567 GOSPER : 40034.4823215513

In reply to Re: Stirling Approx to N! for large Number? by tall_man
in thread Stirling Approx to N! for large Number? by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (1)
    As of 2018-07-19 06:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (403 votes). Check out past polls.

      Notices?