September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,14 @@
|
|||
# syntax: GAWK -f EVALUATE_BINOMIAL_COEFFICIENTS.AWK
|
||||
BEGIN {
|
||||
main(5,3)
|
||||
main(100,2)
|
||||
main(33,17)
|
||||
exit(0)
|
||||
}
|
||||
function main(n,k, i,r) {
|
||||
r = 1
|
||||
for (i=1; i<k+1; i++) {
|
||||
r *= (n - i + 1) / i
|
||||
}
|
||||
printf("%d %d = %d\n",n,k,r)
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
typedef unsigned long ULONG;
|
||||
|
||||
ULONG binomial(ULONG n, ULONG k)
|
||||
{
|
||||
ULONG r = 1, d = n - k;
|
||||
|
||||
/* choose the smaller of k and n - k */
|
||||
if (d > k) { k = d; d = n - k; }
|
||||
|
||||
while (n > k) {
|
||||
if (r >= UINT_MAX / n) return 0; /* overflown */
|
||||
r *= n--;
|
||||
|
||||
/* divide (n - k)! as soon as we can to delay overflows */
|
||||
while (d > 1 && !(r % d)) r /= d--;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("%lu\n", binomial(5, 3));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
10
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
binomial_coefficient = (n, k) ->
|
||||
result = 1
|
||||
for i in [0...k]
|
||||
result *= (n - i) / (i + 1)
|
||||
result
|
||||
|
||||
n = 5
|
||||
for k in [0..n]
|
||||
console.log "binomial_coefficient(#{n}, #{k}) = #{binomial_coefficient(n,k)}"
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
> coffee binomial.coffee
|
||||
binomial_coefficient(5, 0) = 1
|
||||
binomial_coefficient(5, 1) = 5
|
||||
binomial_coefficient(5, 2) = 10
|
||||
binomial_coefficient(5, 3) = 10
|
||||
binomial_coefficient(5, 4) = 5
|
||||
binomial_coefficient(5, 5) = 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
[sx1q]sz[d0=zd1-lfx*]sf[skdlfxrlk-lfxlklfx*/]sb
|
||||
|
|
@ -0,0 +1 @@
|
|||
5 3lbxp
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
[ macro z: factorial base case when n is (z)ero ]sx
|
||||
[sx [ x is our dump register; get rid of extraneous copy of n we no longer need]sx
|
||||
1 [ return value is 1 ]sx
|
||||
q] [ abort processing of calling macro ]sx
|
||||
sz
|
||||
|
||||
[ macro f: factorial ]sx [
|
||||
d [ duplicate the input (n) ]sx
|
||||
0 =z [ if n is zero, call z, which stops here and returns 1 ]sx
|
||||
d [ otherwise, duplicate n again ]sx
|
||||
1 - [ subtract 1 ]sx
|
||||
lfx [ take the factorial ]sx
|
||||
* [ we have (n-1)!; multiply it by the copy of n to get n! ]sx
|
||||
] sf
|
||||
|
||||
[ macro b(n,k): binomial function (n choose k).
|
||||
straightforward RPN version of formula.]sx [
|
||||
sk [ remember k. stack: n ]sx
|
||||
d [ duplicate: n n ]sx
|
||||
lfx [ call factorial: n n! ]sx
|
||||
r [ swap: n! n ]sx
|
||||
lk [ load k: n! n k ]sx
|
||||
- [ subtract: n! n-k ]sx
|
||||
lfx [ call factorial: n! (n-k)! ]sx
|
||||
lk [ load k: n! (n-k)! k ]sx
|
||||
lfx [ call factorial; n! (n-k)! k! ]sx
|
||||
* [ multiply: n! (n-k)!k! ]sx
|
||||
/ [ divide: n!/(n-k)!k! ]sx
|
||||
] sb
|
||||
|
||||
5 3 lb x p [print(5 choose 3)]sx
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
program test_choose
|
||||
|
||||
implicit none
|
||||
|
||||
write (*, '(i0)') choose (5, 3)
|
||||
|
||||
contains
|
||||
|
||||
function factorial (n) result (res)
|
||||
|
||||
implicit none
|
||||
integer, intent (in) :: n
|
||||
integer :: res
|
||||
integer :: i
|
||||
|
||||
res = product ((/(i, i = 1, n)/))
|
||||
|
||||
end function factorial
|
||||
|
||||
function choose (n, k) result (res)
|
||||
|
||||
implicit none
|
||||
integer, intent (in) :: n
|
||||
integer, intent (in) :: k
|
||||
integer :: res
|
||||
|
||||
res = factorial (n) / (factorial (k) * factorial (n - k))
|
||||
|
||||
end function choose
|
||||
|
||||
end program test_choose
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
fun factorial(n: Int) = when {
|
||||
n < 0 -> throw IllegalArgumentException("negative numbers not allowed")
|
||||
else -> {
|
||||
var ans = 1L
|
||||
for (i in 2..n) ans *= i
|
||||
ans
|
||||
}
|
||||
}
|
||||
|
||||
fun binomial(n: Int, k: Int) = when {
|
||||
n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed")
|
||||
n == k -> 1L
|
||||
else -> {
|
||||
var ans = 1L
|
||||
for (i in n - k + 1..n) ans *= i
|
||||
ans / factorial(k)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for (n in 0..14) {
|
||||
for (k in 0..n)
|
||||
print("%4d ".format(binomial(n, k)))
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
local Binomial = setmetatable({},{
|
||||
__call = function(self,n,k)
|
||||
local hash = (n<<32) | (k & 0xffffffff)
|
||||
local ans = self[hash]
|
||||
if not ans then
|
||||
if n<0 or k>n then
|
||||
return 0 -- not save
|
||||
elseif n<=1 or k==0 or k==n then
|
||||
ans = 1
|
||||
else
|
||||
if 2*k > n then
|
||||
ans = self(n, n - k)
|
||||
else
|
||||
local lhs = self(n-1,k)
|
||||
local rhs = self(n-1,k-1)
|
||||
local sum = lhs + rhs
|
||||
if sum<0 or not math.tointeger(sum)then
|
||||
-- switch to double
|
||||
ans = lhs/1.0 + rhs/1.0 -- approximate
|
||||
else
|
||||
ans = sum
|
||||
end
|
||||
end
|
||||
end
|
||||
rawset(self,hash,ans)
|
||||
end
|
||||
return ans
|
||||
end
|
||||
})
|
||||
print( Binomial(100,50)) -- 1.0089134454556e+029
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
binomial_coefficients:
|
||||
procedure options (main);
|
||||
declare (n, k) fixed;
|
||||
|
||||
get (n, k);
|
||||
put (coefficient(n, k));
|
||||
|
||||
coefficient: procedure (n, k) returns (fixed decimal (15));
|
||||
declare (n, k) fixed;
|
||||
return (fact(n)/ (fact(n-k) * fact(k)) );
|
||||
end coefficient;
|
||||
|
||||
fact: procedure (n) returns (fixed decimal (15));
|
||||
declare n fixed;
|
||||
declare i fixed, f fixed decimal (15);
|
||||
f = 1;
|
||||
do i = 1 to n;
|
||||
f = f * i;
|
||||
end;
|
||||
return (f);
|
||||
end fact;
|
||||
end binomial_coefficients;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function binom(integer n, k)
|
||||
return factorial(n)/(factorial(k)*factorial(n-k))
|
||||
end function
|
||||
|
||||
?binom(5,3)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
include builtins\bigatom.e
|
||||
|
||||
function ba_binom(integer n, k)
|
||||
bigatom r = ba_new(1)
|
||||
for i=1 to k do
|
||||
r = ba_divide(ba_multiply(r,n-i+1),i)
|
||||
end for
|
||||
return r
|
||||
end function
|
||||
|
||||
?ba_sprintf("%B",ba_binom(5,3))
|
||||
?ba_sprintf("%B",ba_binom(100,50))
|
||||
?ba_sprintf("%B",ba_binom(60,30))
|
||||
?ba_sprintf("%B",ba_binom(1200,120))
|
||||
|
|
@ -1 +0,0 @@
|
|||
choose(5,3)
|
||||
|
|
@ -1 +0,0 @@
|
|||
[1] 10
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
java Binomial 100 30
|
||||
The Binomial Coefficient of 100 and 30 equals 29,372,339,821,610,944,823,963,760.
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
def bico(n: Long, k: Long): Long = (n, k) match {
|
||||
case (n, 0) => 1
|
||||
case (0, k) => 0
|
||||
case (n, k) => bico(n - 1, k - 1) + bico(n - 1, k)
|
||||
}
|
||||
println("bico(5,3) = " + bico(5, 3))
|
||||
|
|
@ -1 +0,0 @@
|
|||
bico(5,3) = 10
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
(define (factorial n)
|
||||
(define (*factorial n acc)
|
||||
(if (zero? n)
|
||||
acc
|
||||
(*factorial (- n 1) (* acc n))))
|
||||
(*factorial n 1))
|
||||
|
||||
(define (choose n k)
|
||||
(/ (factorial n) (* (factorial k) (factorial (- n k)))))
|
||||
|
||||
(display (choose 5 3))
|
||||
(newline)
|
||||
|
|
@ -0,0 +1 @@
|
|||
choose(n, k) := product(k + 1 ... n) / product(1 ... n - k);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
choose(n,k) := binomial(n, k, 1, 1);
|
||||
|
||||
binomial(n, k, i, result) :=
|
||||
result when i > k else
|
||||
binomial(n, k, i + 1, (result * (n - i + 1)) / i);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
. display comb(5,3)
|
||||
10
|
||||
|
|
@ -0,0 +1 @@
|
|||
fcn binomial(n,k){ (1).reduce(k,fcn(p,i,n){ p*(n-i+1)/i },1,n) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue