Last active
March 7, 2024 12:37
-
-
Save tueda/303fdf7f748e0643dcbbef39fa6a5406 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returns NaN. | |
function nan() { | |
return "+NaN" + 0 | |
} | |
# Returns the binomial coefficient. | |
# It works, at least, when either n or k is a non-negative integer. | |
function binom(n, k, tmp) { | |
if (n == k || k == 0) { | |
return 1 | |
} | |
if (k > n - k && n - k >= 0) { | |
k = n - k | |
} | |
tmp = 1 | |
while (n >= 1 && k >= 1) { | |
tmp *= n / k | |
n-- | |
k-- | |
} | |
while (k >= 1) { | |
tmp *= (n - k + 1) / k | |
k-- | |
} | |
while (n >= 1 && n > k) { | |
tmp *= n / (n - k) | |
n-- | |
} | |
if (n == k || k == 0) { | |
return tmp | |
} | |
if (n == 0) { | |
k *= atan2(0, -1) | |
return tmp *= sin(k) / k | |
} | |
return nan() # unsupported | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment