summaryrefslogtreecommitdiff
path: root/sterling_approx.awk
blob: 5abe41d76075b2644c7e3976d52a90f48c00eab6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)
}