summaryrefslogtreecommitdiff
path: root/sterling_approx.awk
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sterling_approx.awk17
1 files changed, 17 insertions, 0 deletions
diff --git a/sterling_approx.awk b/sterling_approx.awk
new file mode 100644
index 0000000..5abe41d
--- /dev/null
+++ b/sterling_approx.awk
@@ -0,0 +1,17 @@
+#!/usr/bin/awk -f
+
+### 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
+ 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))
+ }
+ printf(OFMT ORS, p)
+}