No BSD License  

Highlights from
Numerical Methods for Physics

from Numerical Methods for Physics by Alejandro Garcia
Companion Software

facts.m
% facts - Program for computing factorial 
% using Sterling's approximation
clear;  help facts;  % Clear memory and print header
n = input('Enter value of n - ');
if( n < 30 )  % For small values of n use product
  nf = 1;
  for i=1:n
    nf = nf*i;
  end
  fprintf(' n! = %g \n',nf);
else          % For larger values use Sterling's apprx.
  log_nf = .5*log10(2*pi) + (n+.5)*log10(n) ...
           - n*log10(exp(1)) + log10(1+1/(12*n)+1/(288*n^2));
  exponnt = floor(log_nf);
  mantisa = 10^(log_nf - exponnt);
  fprintf(' n! = %g e %g \n',mantisa,exponnt);
end

Contact us at files@mathworks.com