From 72fdb25210c579beaabc35cae7ec803436887f20 Mon Sep 17 00:00:00 2001 From: wukong Date: Sat, 20 Oct 2018 11:47:10 -0700 Subject: merged lin_reg python files, added comment on Ramanujan's version of gamma function to sterling_approx; --- sterling_approx.awk | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'sterling_approx.awk') diff --git a/sterling_approx.awk b/sterling_approx.awk index 5abe41d..ca119ac 100644 --- a/sterling_approx.awk +++ b/sterling_approx.awk @@ -1,17 +1,23 @@ #!/usr/bin/awk -f + +# https://en.wikipedia.org/wiki/Sterling_Approximation +# An alternative approximation for the Gamma function stated by Srinivasa +# Ramanujan (Ramanujan 1988) is +# Gamma(1+x) ~= sqrt(pi)((x/e)^x)(8x^3 + 4x^2 + x + 1/30)^(1/6) +# for x >= 0. The equivalent approximation for ln(n!) has an asymptotic error +# of 1/(1400*n^3) ... + + ### sterling_approx.awk # https://en.wikipedia.org/wiki/Stirling%27s_approximation BEGIN { ARGV[1] ? n = ARGV[1] : n = 0 pi = 4*atan2(1,1) - p = 0 + f = 0 if (n > 0) { - p = 1 - for (m=n; m>0; m--) - p *= n*exp(-1) - p = sqrt(2*pi*n)*p*(1 + 1/(12*n) + 1/(288*n*n) - 139/(51840*n*n*n) - 571/(2488320*n*n*n*n)) + f = sqrt(2*pi*n)*exp(n*log(n*exp(-1)))*(1 + 1/(12*n) + 1/(288*n*n) - 139/(51840*n*n*n) - 571/(2488320*n*n*n*n)) } - printf(OFMT ORS, p) + printf(OFMT ORS, f) } -- cgit v1.2.3