Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Evaluate-binomial-coefficients/00-META.yaml
Normal file
3
Task/Evaluate-binomial-coefficients/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Evaluate_binomial_coefficients
|
||||
note: Mathematical operations
|
||||
16
Task/Evaluate-binomial-coefficients/00-TASK.txt
Normal file
16
Task/Evaluate-binomial-coefficients/00-TASK.txt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
This programming task, is to calculate ANY binomial coefficient.
|
||||
|
||||
However, it has to be able to output <big><big><math>\binom{5}{3}</math></big></big>, which is '''10'''.
|
||||
|
||||
This formula is recommended:
|
||||
<big><big>
|
||||
:: <math>\binom{n}{k} = \frac{n!}{(n-k)!k!} = \frac{n(n-1)(n-2)\ldots(n-k+1)}{k(k-1)(k-2)\ldots 1}</math>
|
||||
</big></big>
|
||||
|
||||
|
||||
'''See Also:'''
|
||||
* [[Combinations and permutations]]
|
||||
* [[Pascal's triangle]]
|
||||
{{Template:Combinations and permutations}}
|
||||
<br>
|
||||
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
F binomial_coeff(n, k)
|
||||
V result = 1
|
||||
L(i) 1..k
|
||||
result = result * (n - i + 1) / i
|
||||
R result
|
||||
|
||||
print(binomial_coeff(5, 3))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
* Evaluate binomial coefficients - 29/09/2015
|
||||
BINOMIAL CSECT
|
||||
USING BINOMIAL,R15 set base register
|
||||
SR R4,R4 clear for mult and div
|
||||
LA R5,1 r=1
|
||||
LA R7,1 i=1
|
||||
L R8,N m=n
|
||||
LOOP LR R4,R7 do while i<=k
|
||||
C R4,K i<=k
|
||||
BH LOOPEND if not then exit while
|
||||
MR R4,R8 r*m
|
||||
DR R4,R7 r=r*m/i
|
||||
LA R7,1(R7) i=i+1
|
||||
BCTR R8,0 m=m-1
|
||||
B LOOP loop while
|
||||
LOOPEND XDECO R5,PG edit r
|
||||
XPRNT PG,12 print r
|
||||
XR R15,R15 set return code
|
||||
BR R14 return to caller
|
||||
N DC F'10' <== input value
|
||||
K DC F'4' <== input value
|
||||
PG DS CL12 buffer
|
||||
YREGS
|
||||
END BINOMIAL
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
CLASS lcl_binom DEFINITION CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
CLASS-METHODS:
|
||||
calc
|
||||
IMPORTING n TYPE i
|
||||
k TYPE i
|
||||
RETURNING VALUE(r_result) TYPE f.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
CLASS lcl_binom IMPLEMENTATION.
|
||||
|
||||
METHOD calc.
|
||||
|
||||
r_result = 1.
|
||||
DATA(i) = 1.
|
||||
DATA(m) = n.
|
||||
|
||||
WHILE i <= k.
|
||||
r_result = r_result * m / i.
|
||||
i = i + 1.
|
||||
m = m - 1.
|
||||
ENDWHILE.
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defun fac (n)
|
||||
(if (zp n)
|
||||
1
|
||||
(* n (fac (1- n)))))
|
||||
|
||||
(defun binom (n k)
|
||||
(/ (fac n) (* (fac (- n k)) (fac k)))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
PROC factorial = (INT n)INT:
|
||||
(
|
||||
INT result;
|
||||
|
||||
result := 1;
|
||||
FOR i TO n DO
|
||||
result *:= i
|
||||
OD;
|
||||
|
||||
result
|
||||
);
|
||||
|
||||
PROC choose = (INT n, INT k)INT:
|
||||
(
|
||||
INT result;
|
||||
|
||||
# Note: code can be optimised here as k < n #
|
||||
result := factorial(n) OVER (factorial(k) * factorial(n - k));
|
||||
|
||||
result
|
||||
);
|
||||
|
||||
test:(
|
||||
print((choose(5, 3), new line))
|
||||
)
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
begin
|
||||
% calculates n!/k! %
|
||||
integer procedure factorialOverFactorial( integer value n, k ) ;
|
||||
if k > n then 0
|
||||
else if k = n then 1
|
||||
else % k < n % begin
|
||||
integer f;
|
||||
f := 1;
|
||||
for i := k + 1 until n do f := f * i;
|
||||
f
|
||||
end factorialOverFactorial ;
|
||||
|
||||
% calculates n! %
|
||||
integer procedure factorial( integer value n ) ;
|
||||
begin
|
||||
integer f;
|
||||
f := 1;
|
||||
for i := 2 until n do f := f * i;
|
||||
f
|
||||
end factorial ;
|
||||
|
||||
% calculates the binomial coefficient of (n k) %
|
||||
% uses the factorialOverFactorial procedure for a slight optimisation %
|
||||
integer procedure binomialCoefficient( integer value n, k ) ;
|
||||
if ( n - k ) > k
|
||||
then factorialOverFactorial( n, n - k ) div factorial( k )
|
||||
else factorialOverFactorial( n, k ) div factorial( n - k );
|
||||
|
||||
% display the binomial coefficient of (5 3) %
|
||||
write( binomialCoefficient( 5, 3 ) )
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
3!5
|
||||
10
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
procedure Test_Binomial is
|
||||
function Binomial (N, K : Natural) return Natural is
|
||||
Result : Natural := 1;
|
||||
M : Natural;
|
||||
begin
|
||||
if N < K then
|
||||
raise Constraint_Error;
|
||||
end if;
|
||||
if K > N/2 then -- Use symmetry
|
||||
M := N - K;
|
||||
else
|
||||
M := K;
|
||||
end if;
|
||||
for I in 1..M loop
|
||||
Result := Result * (N - M + I) / I;
|
||||
end loop;
|
||||
return Result;
|
||||
end Binomial;
|
||||
begin
|
||||
for N in 0..17 loop
|
||||
for K in 0..N loop
|
||||
Put (Integer'Image (Binomial (N, K)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Test_Binomial;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
set n to 5
|
||||
set k to 3
|
||||
|
||||
on calculateFactorial(val)
|
||||
set partial_factorial to 1 as integer
|
||||
repeat with i from 1 to val
|
||||
set factorial to i * partial_factorial
|
||||
set partial_factorial to factorial
|
||||
end repeat
|
||||
return factorial
|
||||
end calculateFactorial
|
||||
|
||||
set n_factorial to calculateFactorial(n)
|
||||
set k_factorial to calculateFactorial(k)
|
||||
set n_minus_k_factorial to calculateFactorial(n - k)
|
||||
|
||||
return n_factorial / (n_minus_k_factorial) * 1 / (k_factorial) as integer
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
-- factorial :: Int -> Int
|
||||
on factorial(n)
|
||||
product(enumFromTo(1, n))
|
||||
end factorial
|
||||
|
||||
|
||||
-- binomialCoefficient :: Int -> Int -> Int
|
||||
on binomialCoefficient(n, k)
|
||||
factorial(n) div (factorial(n - k) * (factorial(k)))
|
||||
end binomialCoefficient
|
||||
|
||||
-- Or, by reduction:
|
||||
|
||||
-- binomialCoefficient2 :: Int -> Int -> Int
|
||||
on binomialCoefficient2(n, k)
|
||||
product(enumFromTo(1 + k, n)) div (factorial(n - k))
|
||||
end binomialCoefficient2
|
||||
|
||||
|
||||
-- TEST -----------------------------------------------------
|
||||
on run
|
||||
|
||||
{binomialCoefficient(5, 3), binomialCoefficient2(5, 3)}
|
||||
|
||||
--> {10, 10}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERAL -------------------------------------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set lst to {}
|
||||
repeat with i from m to n
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
else
|
||||
return {}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- product :: [Num] -> Num
|
||||
on product(xs)
|
||||
script multiply
|
||||
on |λ|(a, b)
|
||||
a * b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(multiply, 1, xs)
|
||||
end product
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
factorial: function [n]-> product 1..n
|
||||
binomial: function [x,y]-> (factorial x) / (factorial y) * factorial x-y
|
||||
|
||||
print binomial 5 3
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
MsgBox, % Round(BinomialCoefficient(5, 3))
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
BinomialCoefficient(n, k) {
|
||||
;---------------------------------------------------------------------------
|
||||
r := 1
|
||||
Loop, % k < n - k ? k : n - k {
|
||||
r *= n - A_Index + 1
|
||||
r /= A_Index
|
||||
}
|
||||
Return, r
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
@%=&1010
|
||||
|
||||
PRINT "Binomial (5,3) = "; FNbinomial(5, 3)
|
||||
PRINT "Binomial (100,2) = "; FNbinomial(100, 2)
|
||||
PRINT "Binomial (33,17) = "; FNbinomial(33, 17)
|
||||
END
|
||||
|
||||
DEF FNbinomial(N%, K%)
|
||||
LOCAL R%, D%
|
||||
R% = 1 : D% = N% - K%
|
||||
IF D% > K% THEN K% = D% : D% = N% - K%
|
||||
WHILE N% > K%
|
||||
R% *= N%
|
||||
N% -= 1
|
||||
WHILE D% > 1 AND (R% MOD D%) = 0
|
||||
R% /= D%
|
||||
D% -= 1
|
||||
ENDWHILE
|
||||
ENDWHILE
|
||||
= R%
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
GET "libhdr"
|
||||
|
||||
LET choose(n, k) =
|
||||
~(0 <= k <= n) -> 0,
|
||||
2*k > n -> binomial(n, n - k),
|
||||
binomial(n, k)
|
||||
|
||||
AND binomial(n, k) =
|
||||
k = 0 -> 1,
|
||||
binomial(n, k - 1) * (n - k + 1) / k
|
||||
|
||||
LET start() = VALOF {
|
||||
LET n, k = ?, ?
|
||||
LET argv = VEC 20
|
||||
LET sz = ?
|
||||
|
||||
sz := rdargs("n/a/n/p,k/a/n/p", argv, 20)
|
||||
UNLESS sz ~= 0 RESULTIS 1
|
||||
|
||||
n := !argv!0
|
||||
k := !argv!1
|
||||
|
||||
writef("%d choose %d = %d *n", n, k, choose(n, k))
|
||||
RESULTIS 0
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
@echo off & setlocal
|
||||
|
||||
if "%~2"=="" ( echo Usage: %~nx0 n k && goto :EOF )
|
||||
|
||||
call :binom binom %~1 %~2
|
||||
1>&2 set /P "=%~1 choose %~2 = "<NUL
|
||||
echo %binom%
|
||||
|
||||
goto :EOF
|
||||
|
||||
:binom <var_to_set> <N> <K>
|
||||
setlocal
|
||||
set /a coeff=1, nk=%~2 - %~3 + 1
|
||||
for /L %%I in (%nk%, 1, %~2) do set /a coeff *= %%I
|
||||
for /L %%I in (1, 1, %~3) do set /a coeff /= %%I
|
||||
endlocal && set "%~1=%coeff%"
|
||||
goto :EOF
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
(binomial=
|
||||
n k coef
|
||||
. !arg:(?n,?k)
|
||||
& (!n+-1*!k:<!k:?k|)
|
||||
& 1:?coef
|
||||
& whl
|
||||
' ( !k:>0
|
||||
& !coef*!n*!k^-1:?coef
|
||||
& !k+-1:?k
|
||||
& !n+-1:?n
|
||||
)
|
||||
& !coef
|
||||
);
|
||||
|
||||
binomial$(5,3)
|
||||
10
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) 5 3nr
|
||||
10
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
double Factorial(double nValue)
|
||||
{
|
||||
double result = nValue;
|
||||
double result_next;
|
||||
double pc = nValue;
|
||||
do
|
||||
{
|
||||
result_next = result*(pc-1);
|
||||
result = result_next;
|
||||
pc--;
|
||||
}while(pc>2);
|
||||
nValue = result;
|
||||
return nValue;
|
||||
}
|
||||
|
||||
double binomialCoefficient(double n, double k)
|
||||
{
|
||||
if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0;
|
||||
if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n;
|
||||
return Factorial(n) /(Factorial(k)*Factorial((n - k)));
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
int main()
|
||||
{
|
||||
cout<<"The Binomial Coefficient of 5, and 3, is equal to: "<< binomialCoefficient(5,3);
|
||||
cin.get();
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
|
||||
namespace BinomialCoefficients
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
ulong n = 1000000, k = 3;
|
||||
ulong result = biCoefficient(n, k);
|
||||
Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result);
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static int fact(int n)
|
||||
{
|
||||
if (n == 0) return 1;
|
||||
else return n * fact(n - 1);
|
||||
}
|
||||
|
||||
static ulong biCoefficient(ulong n, ulong k)
|
||||
{
|
||||
if (k > n - k)
|
||||
{
|
||||
k = n - k;
|
||||
}
|
||||
|
||||
ulong c = 1;
|
||||
for (uint i = 0; i < k; i++)
|
||||
{
|
||||
c = c * (n - i);
|
||||
c = c / (i + 1);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
/* We go to some effort to handle overflow situations */
|
||||
|
||||
static unsigned long gcd_ui(unsigned long x, unsigned long y) {
|
||||
unsigned long t;
|
||||
if (y < x) { t = x; x = y; y = t; }
|
||||
while (y > 0) {
|
||||
t = y; y = x % y; x = t; /* y1 <- x0 % y0 ; x1 <- y0 */
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
unsigned long binomial(unsigned long n, unsigned long k) {
|
||||
unsigned long d, g, r = 1;
|
||||
if (k == 0) return 1;
|
||||
if (k == 1) return n;
|
||||
if (k >= n) return (k == n);
|
||||
if (k > n/2) k = n-k;
|
||||
for (d = 1; d <= k; d++) {
|
||||
if (r >= ULONG_MAX/n) { /* Possible overflow */
|
||||
unsigned long nr, dr; /* reduced numerator / denominator */
|
||||
g = gcd_ui(n, d); nr = n/g; dr = d/g;
|
||||
g = gcd_ui(r, dr); r = r/g; dr = dr/g;
|
||||
if (r >= ULONG_MAX/nr) return 0; /* Unavoidable overflow */
|
||||
r *= nr;
|
||||
r /= dr;
|
||||
n--;
|
||||
} else {
|
||||
r *= n--;
|
||||
r /= d;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%lu\n", binomial(5, 3));
|
||||
printf("%lu\n", binomial(40, 19));
|
||||
printf("%lu\n", binomial(67, 31));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defn binomial-coefficient [n k]
|
||||
(let [rprod (fn [a b] (reduce * (range a (inc b))))]
|
||||
(/ (rprod (- n k -1) n) (rprod 1 k))))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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)}"
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
10 REM BINOMIAL COEFFICIENTS
|
||||
20 REM COMMODORE BASIC 2.0
|
||||
30 REM 2021-08-24
|
||||
40 REM BY ALVALONGO
|
||||
100 Z=0:U=1
|
||||
110 FOR N=U TO 10
|
||||
120 PRINT N;
|
||||
130 FOR K=Z TO N
|
||||
140 GOSUB 900
|
||||
150 PRINT C;
|
||||
160 NEXT K
|
||||
170 PRINT
|
||||
180 NEXT N
|
||||
190 END
|
||||
900 REM BINOMIAL COEFFICIENT
|
||||
910 IF K<Z OR K>N THEN C=Z:RETURN
|
||||
920 IF K=Z OR K=N THEN C=U:RETURN
|
||||
930 P=K:IF N-K<P THEN P=N-K
|
||||
940 C=U
|
||||
950 FOR I=Z TO P-U
|
||||
960 C=C/(I+U)*(N-I)
|
||||
980 NEXT I
|
||||
990 RETURN
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defun choose (n k)
|
||||
(labels ((prod-enum (s e)
|
||||
(do ((i s (1+ i)) (r 1 (* i r))) ((> i e) r)))
|
||||
(fact (n) (prod-enum 1 n)))
|
||||
(/ (prod-enum (- (1+ n) k) n) (fact k))))
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
T binomial(T)(in T n, T k) pure nothrow {
|
||||
if (k > (n / 2))
|
||||
k = n - k;
|
||||
T bc = 1;
|
||||
foreach (T i; T(2) .. k + 1)
|
||||
bc = (bc * (n - k + i)) / i;
|
||||
return bc;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.bigint;
|
||||
|
||||
foreach (const d; [[5, 3], [100, 2], [100, 98]])
|
||||
writefln("(%3d %3d) = %s", d[0], d[1], binomial(d[0], d[1]));
|
||||
writeln("(100 50) = ", binomial(100.BigInt, 50.BigInt));
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
T BinomialCoeff(T)(in T n, in T k)
|
||||
{
|
||||
T nn = n, kk = k, c = cast(T)1;
|
||||
|
||||
if (kk > nn - kk) kk = nn - kk;
|
||||
|
||||
for (T i = cast(T)0; i < kk; i++)
|
||||
{
|
||||
c = c * (nn - i);
|
||||
c = c / (i + cast(T)1);
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
import std.stdio, std.bigint;
|
||||
|
||||
BinomialCoeff(10UL, 3UL).writeln;
|
||||
BinomialCoeff(100.BigInt, 50.BigInt).writeln;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
program Binomial;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
function BinomialCoff(N, K: Cardinal): Cardinal;
|
||||
var
|
||||
L: Cardinal;
|
||||
|
||||
begin
|
||||
if N < K then
|
||||
Result:= 0 // Error
|
||||
else begin
|
||||
if K > N - K then
|
||||
K:= N - K; // Optimization
|
||||
Result:= 1;
|
||||
L:= 0;
|
||||
while L < K do begin
|
||||
Result:= Result * (N - L);
|
||||
Inc(L);
|
||||
Result:= Result div L;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
begin
|
||||
Writeln('C(5,3) is ', BinomialCoff(5, 3));
|
||||
ReadLn;
|
||||
end.
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
PROGRAM BINOMIAL
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
PROCEDURE BINOMIAL(N,K->BIN)
|
||||
LOCAL R,D
|
||||
R=1 D=N-K
|
||||
IF D>K THEN K=D D=N-K END IF
|
||||
WHILE N>K DO
|
||||
R*=N
|
||||
N-=1
|
||||
WHILE D>1 AND (R-D*INT(R/D))=0 DO
|
||||
R/=D
|
||||
D-=1
|
||||
END WHILE
|
||||
END WHILE
|
||||
BIN=R
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
BINOMIAL(5,3->BIN)
|
||||
PRINT("Binomial (5,3) = ";BIN)
|
||||
BINOMIAL(100,2->BIN)
|
||||
PRINT("Binomial (100,2) = ";BIN)
|
||||
BINOMIAL(33,17->BIN)
|
||||
PRINT("Binomial (33,17) = ";BIN)
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
defmodule RC do
|
||||
def choose(n,k) when is_integer(n) and is_integer(k) and n>=0 and k>=0 and n>=k do
|
||||
if k==0, do: 1, else: choose(n,k,1,1)
|
||||
end
|
||||
|
||||
def choose(n,k,k,acc), do: div(acc * (n-k+1), k)
|
||||
def choose(n,k,i,acc), do: choose(n, k, i+1, div(acc * (n-i+1), i))
|
||||
end
|
||||
|
||||
IO.inspect RC.choose(5,3)
|
||||
IO.inspect RC.choose(60,30)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
choose(N, 0) -> 1;
|
||||
choose(N, K) when is_integer(N), is_integer(K), (N >= 0), (K >= 0), (N >= K) ->
|
||||
choose(N, K, 1, 1).
|
||||
|
||||
choose(N, K, K, Acc) ->
|
||||
(Acc * (N-K+1)) div K;
|
||||
choose(N, K, I, Acc) ->
|
||||
choose(N, K, I+1, (Acc * (N-I+1)) div I).
|
||||
|
|
@ -0,0 +1 @@
|
|||
let choose n k = List.fold (fun s i -> s * (n-i+1)/i ) 1 [1..k]
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
: fact ( n -- n-factorial )
|
||||
dup 0 = [ drop 1 ] [ dup 1 - fact * ] if ;
|
||||
|
||||
: choose ( n k -- n-choose-k )
|
||||
2dup - [ fact ] tri@ * / ;
|
||||
|
||||
! outputs 10
|
||||
5 3 choose .
|
||||
|
||||
! alternative using folds
|
||||
USE: math.ranges
|
||||
|
||||
! (product [n..k+1] / product [n-k..1])
|
||||
: choose-fold ( n k -- n-choose-k )
|
||||
2dup 1 + [a,b] product -rot - 1 [a,b] product / ;
|
||||
|
|
@ -0,0 +1 @@
|
|||
Bin(5,3)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
: choose ( n k -- nCk ) 1 swap 0 ?do over i - i 1+ */ loop nip ;
|
||||
|
||||
5 3 choose . \ 10
|
||||
33 17 choose . \ 1166803110
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
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,43 @@
|
|||
program binomial
|
||||
integer :: i, j
|
||||
|
||||
do j=1,20
|
||||
write(*,fmt='(i2,a)',advance='no') j,'Cr = '
|
||||
do i=0,j
|
||||
write(*,fmt='(i0,a)',advance='no') n_C_r(j,i),' '
|
||||
end do
|
||||
write(*,'(a,i0)') ' 60C30 = ',n_C_r(60,30)
|
||||
end do
|
||||
stop
|
||||
|
||||
contains
|
||||
|
||||
pure function n_C_r(n, r) result(bin)
|
||||
integer(16) :: bin
|
||||
integer, intent(in) :: n
|
||||
integer, intent(in) :: r
|
||||
|
||||
integer(16) :: num
|
||||
integer(16) :: den
|
||||
integer :: i
|
||||
integer :: k
|
||||
integer, parameter :: primes(*) = [2,3,5,7,11,13,17,19]
|
||||
num = 1
|
||||
den = 1
|
||||
do i=0,r-1
|
||||
num = num*(n-i)
|
||||
den = den*(i+1)
|
||||
if (i > 0) then
|
||||
! Divide out common prime factors
|
||||
do k=1,size(primes)
|
||||
if (mod(i,primes(k)) == 0) then
|
||||
num = num/primes(k)
|
||||
den = den/primes(k)
|
||||
end if
|
||||
end do
|
||||
end if
|
||||
end do
|
||||
bin = num/den
|
||||
end function n_C_r
|
||||
|
||||
end program binomial
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function factorial(n As Integer) As Integer
|
||||
If n < 1 Then Return 1
|
||||
Dim product As Integer = 1
|
||||
For i As Integer = 2 To n
|
||||
product *= i
|
||||
Next
|
||||
Return Product
|
||||
End Function
|
||||
|
||||
Function binomial(n As Integer, k As Integer) As Integer
|
||||
If n < 0 OrElse k < 0 OrElse n <= k Then Return 1
|
||||
Dim product As Integer = 1
|
||||
For i As Integer = n - k + 1 To n
|
||||
Product *= i
|
||||
Next
|
||||
Return product \ factorial(k)
|
||||
End Function
|
||||
|
||||
For n As Integer = 0 To 14
|
||||
For k As Integer = 0 To n
|
||||
Print Using "####"; binomial(n, k);
|
||||
Print" ";
|
||||
Next k
|
||||
Print
|
||||
Next n
|
||||
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1 @@
|
|||
println[binomial[5,3]]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def
|
||||
choose( n, k ) | k < 0 or k > n = 0
|
||||
choose( n, 0 ) = 1
|
||||
choose( n, n ) = 1
|
||||
choose( n, k ) = product( [(n - i)/(i + 1) | i <- 0:min( k, n - k )] )
|
||||
|
||||
println( choose(5, 3) )
|
||||
println( choose(60, 30) )
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import integers.factorial
|
||||
|
||||
def
|
||||
binomial( n, k ) | k < 0 or k > n = 0
|
||||
binomial( n, k ) = factorial( n )/factorial( n - k )/factorial( k )
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
# Built-in
|
||||
Binomial(5, 3);
|
||||
# 10
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
10 REM BINOMIAL CALCULATOR
|
||||
20 INPUT "N? ", N
|
||||
30 INPUT "P? ", P
|
||||
40 GOSUB 70
|
||||
50 PRINT C
|
||||
60 END
|
||||
70 C = 0
|
||||
80 IF N < 0 OR P<0 OR P > N THEN RETURN
|
||||
90 IF P < N\2 THEN P = N - P
|
||||
100 C = 1
|
||||
110 FOR I = N TO P+1 STEP -1
|
||||
120 C=C*I
|
||||
130 NEXT I
|
||||
140 FOR I = 1 TO N-P
|
||||
150 C=C/I
|
||||
160 NEXT I
|
||||
170 RETURN
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package main
|
||||
import "fmt"
|
||||
import "math/big"
|
||||
|
||||
func main() {
|
||||
fmt.Println(new(big.Int).Binomial(5, 3))
|
||||
fmt.Println(new(big.Int).Binomial(60, 30))
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
;5 3 # Set up demo input
|
||||
{),(;{*}*}:f; # Define a factorial function
|
||||
.f@.f@/\@-f/
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
;5 3 # Set up demo input
|
||||
1\,@{1$-@\*\)/}+/
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
def factorial = { x ->
|
||||
assert x > -1
|
||||
x == 0 ? 1 : (1..x).inject(1G) { BigInteger product, BigInteger factor -> product *= factor }
|
||||
}
|
||||
|
||||
def combinations = { n, k ->
|
||||
assert k >= 0
|
||||
assert n >= k
|
||||
factorial(n).intdiv(factorial(k)*factorial(n-k))
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
assert combinations(20, 0) == combinations(20, 20)
|
||||
assert combinations(20, 10) == (combinations(19, 9) + combinations(19, 10))
|
||||
assert combinations(5, 3) == 10
|
||||
println combinations(5, 3)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
choose :: (Integral a) => a -> a -> a
|
||||
choose n k = product [k+1..n] `div` product [1..n-k]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> 5 `choose` 3
|
||||
10
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
choose :: (Integral a) => a -> a -> a
|
||||
choose n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
coeffs = iterate next [1]
|
||||
where
|
||||
next ns = zipWith (+) (0:ns) $ ns ++ [0]
|
||||
|
||||
main = print $ coeffs !! 5 !! 3
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
WRITE(Messagebox) BinomCoeff( 5, 3) ! displays 10
|
||||
|
||||
FUNCTION factorial( n )
|
||||
factorial = 1
|
||||
DO i = 1, n
|
||||
factorial = factorial * i
|
||||
ENDDO
|
||||
END
|
||||
|
||||
FUNCTION BinomCoeff( n, k )
|
||||
BinomCoeff = factorial(n)/factorial(n-k)/factorial(k)
|
||||
END
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
100 PROGRAM "Binomial.bas"
|
||||
110 PRINT "Binomial (5,3) =";BINOMIAL(5,3)
|
||||
120 DEF BINOMIAL(N,K)
|
||||
130 LET R=1:LET D=N-K
|
||||
140 IF D>K THEN LET K=D:LET D=N-K
|
||||
150 DO WHILE N>K
|
||||
160 LET R=R*N:LET N=N-1
|
||||
170 DO WHILE D>1 AND MOD(R,D)=0
|
||||
180 LET R=R/D:LET D=D-1
|
||||
190 LOOP
|
||||
200 LOOP
|
||||
210 LET BINOMIAL=R
|
||||
220 END DEF
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
link math, factors
|
||||
|
||||
procedure main()
|
||||
write("choose(5,3)=",binocoef(5,3))
|
||||
end
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
procedure binocoef(n, k) #: binomial coefficient
|
||||
|
||||
k := integer(k) | fail
|
||||
n := integer(n) | fail
|
||||
|
||||
if (k = 0) | (n = k) then return 1
|
||||
|
||||
if 0 <= k <= n then
|
||||
return factorial(n) / (factorial(k) * factorial(n - k))
|
||||
else fail
|
||||
|
||||
end
|
||||
|
||||
procedure factorial(n) #: return n! (n factorial)
|
||||
local i
|
||||
|
||||
n := integer(n) | runerr(101, n)
|
||||
|
||||
if n < 0 then fail
|
||||
|
||||
i := 1
|
||||
|
||||
every i *:= 1 to n
|
||||
|
||||
return i
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
3 ! 5
|
||||
10
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
public class Binomial {
|
||||
|
||||
// precise, but may overflow and then produce completely incorrect results
|
||||
private static long binomialInt(int n, int k) {
|
||||
if (k > n - k)
|
||||
k = n - k;
|
||||
|
||||
long binom = 1;
|
||||
for (int i = 1; i <= k; i++)
|
||||
binom = binom * (n + 1 - i) / i;
|
||||
return binom;
|
||||
}
|
||||
|
||||
// same as above, but with overflow check
|
||||
private static Object binomialIntReliable(int n, int k) {
|
||||
if (k > n - k)
|
||||
k = n - k;
|
||||
|
||||
long binom = 1;
|
||||
for (int i = 1; i <= k; i++) {
|
||||
try {
|
||||
binom = Math.multiplyExact(binom, n + 1 - i) / i;
|
||||
} catch (ArithmeticException e) {
|
||||
return "overflow";
|
||||
}
|
||||
}
|
||||
return binom;
|
||||
}
|
||||
|
||||
// using floating point arithmetic, larger numbers can be calculated,
|
||||
// but with reduced precision
|
||||
private static double binomialFloat(int n, int k) {
|
||||
if (k > n - k)
|
||||
k = n - k;
|
||||
|
||||
double binom = 1.0;
|
||||
for (int i = 1; i <= k; i++)
|
||||
binom = binom * (n + 1 - i) / i;
|
||||
return binom;
|
||||
}
|
||||
|
||||
// slow, hard to read, but precise
|
||||
private static BigInteger binomialBigInt(int n, int k) {
|
||||
if (k > n - k)
|
||||
k = n - k;
|
||||
|
||||
BigInteger binom = BigInteger.ONE;
|
||||
for (int i = 1; i <= k; i++) {
|
||||
binom = binom.multiply(BigInteger.valueOf(n + 1 - i));
|
||||
binom = binom.divide(BigInteger.valueOf(i));
|
||||
}
|
||||
return binom;
|
||||
}
|
||||
|
||||
private static void demo(int n, int k) {
|
||||
List<Object> data = Arrays.asList(
|
||||
n,
|
||||
k,
|
||||
binomialInt(n, k),
|
||||
binomialIntReliable(n, k),
|
||||
binomialFloat(n, k),
|
||||
binomialBigInt(n, k));
|
||||
|
||||
System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t")));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
demo(5, 3);
|
||||
demo(1000, 300);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
public class Binomial
|
||||
{
|
||||
private static long binom(int n, int k)
|
||||
{
|
||||
if (k==0)
|
||||
return 1;
|
||||
else if (k>n-k)
|
||||
return binom(n, n-k);
|
||||
else
|
||||
return binom(n-1, k-1)*n/k;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(binom(5, 3));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
function binom(n, k) {
|
||||
var coeff = 1;
|
||||
var i;
|
||||
|
||||
if (k < 0 || k > n) return 0;
|
||||
|
||||
for (i = 0; i < k; i++) {
|
||||
coeff = coeff * (n - i) / (i + 1);
|
||||
}
|
||||
|
||||
return coeff;
|
||||
}
|
||||
|
||||
console.log(binom(5, 3));
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
# nCk assuming n >= k
|
||||
def binomial(n; k):
|
||||
if k > n / 2 then binomial(n; n-k)
|
||||
else reduce range(1; k+1) as $i (1; . * (n - $i + 1) / $i)
|
||||
end;
|
||||
|
||||
def task:
|
||||
.[0] as $n | .[1] as $k
|
||||
| "\($n) C \($k) = \(binomial( $n; $k) )";
|
||||
;
|
||||
|
||||
([5,3], [100,2], [ 33,17]) | task
|
||||
|
|
@ -0,0 +1 @@
|
|||
@show binomial(5, 3)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
function binom(n::Integer, k::Integer)
|
||||
n ≥ k || return 0 # short circuit base cases
|
||||
(n == 1 || k == 0) && return 1
|
||||
|
||||
n * binom(n - 1, k - 1) ÷ k
|
||||
end
|
||||
|
||||
@show binom(5, 3)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
{[n;k]_(*/(k-1)_1+!n)%(*/1+!k)} . 5 3
|
||||
10
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
{[n;k]i:!(k-1);_*/((n-i)%(i+1))} . 5 3
|
||||
10
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
pascal:{x{+':0,x,0}\1}
|
||||
pascal 5
|
||||
(1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1
|
||||
1 5 10 10 5 1)
|
||||
|
||||
{[n;k](pascal n)[n;k]} . 5 3
|
||||
10
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
// version 2.0
|
||||
|
||||
fun binomial(n: Int, k: Int) = when {
|
||||
n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed")
|
||||
n == k -> 1L
|
||||
else -> {
|
||||
val kReduced = min(k, n - k) // minimize number of steps
|
||||
var result = 1L
|
||||
var numerator = n
|
||||
var denominator = 1
|
||||
while (denominator <= kReduced)
|
||||
result = result * numerator-- / denominator++
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
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,29 @@
|
|||
{def C
|
||||
{lambda {:n :p}
|
||||
{/ {* {S.serie :n {- :n :p -1} -1}}
|
||||
{* {S.serie :p 1 -1}}}}}
|
||||
-> C
|
||||
|
||||
{C 16 8}
|
||||
-> 12870
|
||||
|
||||
1{S.map {lambda {:n} {br}1
|
||||
{S.map {C :n} {S.serie 1 {- :n 1}}} 1}
|
||||
{S.serie 2 16}}
|
||||
->
|
||||
1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1
|
||||
1 5 10 10 5 1
|
||||
1 6 15 20 15 6 1
|
||||
1 7 21 35 35 21 7 1
|
||||
1 8 28 56 70 56 28 8 1
|
||||
1 9 36 84 126 126 84 36 9 1
|
||||
1 10 45 120 210 252 210 120 45 10 1
|
||||
1 11 55 165 330 462 462 330 165 55 11 1
|
||||
1 12 66 220 495 792 924 792 495 220 66 12 1
|
||||
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
|
||||
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
|
||||
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
|
||||
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
define binomial(n::integer,k::integer) => {
|
||||
#k == 0 ? return 1
|
||||
local(result = 1)
|
||||
loop(#k) => {
|
||||
#result = #result * (#n - loop_count + 1) / loop_count
|
||||
}
|
||||
return #result
|
||||
}
|
||||
// Tests
|
||||
binomial(5, 3)
|
||||
binomial(5, 4)
|
||||
binomial(60, 30)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
' [RC] Binomial Coefficients
|
||||
|
||||
print "Binomial Coefficient of "; 5; " and "; 3; " is ",BinomialCoefficient( 5, 3)
|
||||
n =1 +int( 10 *rnd( 1))
|
||||
k =1 +int( n *rnd( 1))
|
||||
print "Binomial Coefficient of "; n; " and "; k; " is ",BinomialCoefficient( n, k)
|
||||
|
||||
end
|
||||
|
||||
function BinomialCoefficient( n, k)
|
||||
BinomialCoefficient =factorial( n) /factorial( n -k) /factorial( k)
|
||||
end function
|
||||
|
||||
function factorial( n)
|
||||
if n <2 then
|
||||
f =1
|
||||
else
|
||||
f =n *factorial( n -1)
|
||||
end if
|
||||
factorial =f
|
||||
end function
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
to choose :n :k
|
||||
if :k = 0 [output 1]
|
||||
output (choose :n :k-1) * (:n - :k + 1) / :k
|
||||
end
|
||||
|
||||
show choose 5 3 ; 10
|
||||
show choose 60 30 ; 1.18264581564861e+17
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
function Binomial( n, k )
|
||||
if k > n then return nil end
|
||||
if k > n/2 then k = n - k end -- (n k) = (n n-k)
|
||||
|
||||
numer, denom = 1, 1
|
||||
for i = 1, k do
|
||||
numer = numer * ( n - i + 1 )
|
||||
denom = denom * i
|
||||
end
|
||||
return numer / denom
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
>> nchoosek(5,3)
|
||||
ans =
|
||||
10
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function r = binomcoeff1(n,k)
|
||||
r = diag(rot90(pascal(n+1))); % vector of all binomial coefficients for order n
|
||||
r = r(k);
|
||||
end;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function r = binomcoeff2(n,k)
|
||||
prod((n-k+1:n)./(1:k))
|
||||
end;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function r = binomcoeff3(n,k)
|
||||
m = pascal(max(n-k,k)+1);
|
||||
r = m(n-k+1,k+1);
|
||||
end;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
function coefficients = binomialCoeff(n,k)
|
||||
|
||||
coefficients = zeros(numel(n),numel(k)); %Preallocate memory
|
||||
|
||||
columns = (1:numel(k)); %Preallocate row and column counters
|
||||
rows = (1:numel(n));
|
||||
|
||||
%Iterate over every row and column. The rows represent the n number,
|
||||
%and the columns represent the k number. If n is ever greater than k,
|
||||
%the nchoosek function will throw an error. So, we test to make sure
|
||||
%it isn't, if it is then we leave that entry in the coefficients matrix
|
||||
%zero. Which makes sense combinatorically.
|
||||
for row = rows
|
||||
for col = columns
|
||||
if k(col) <= n(row)
|
||||
coefficients(row,col) = nchoosek(n(row),k(col));
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end %binomialCoeff
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
>> binomialCoeff((0:5),(0:5))
|
||||
|
||||
ans =
|
||||
|
||||
1 0 0 0 0 0
|
||||
1 1 0 0 0 0
|
||||
1 2 1 0 0 0
|
||||
1 3 3 1 0 0
|
||||
1 4 6 4 1 0
|
||||
1 5 10 10 5 1
|
||||
|
||||
>> binomialCoeff([1 0 3 2],(0:3))
|
||||
|
||||
ans =
|
||||
|
||||
1 1 0 0
|
||||
1 0 0 0
|
||||
1 3 3 1
|
||||
1 2 1 0
|
||||
|
||||
>> binomialCoeff(3,(0:3))
|
||||
|
||||
ans =
|
||||
|
||||
1 3 3 1
|
||||
|
||||
>> binomialCoeff((0:3),2)
|
||||
|
||||
ans =
|
||||
|
||||
0
|
||||
0
|
||||
1
|
||||
3
|
||||
|
||||
>> binomialCoeff(5,3)
|
||||
|
||||
ans =
|
||||
|
||||
10
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
// Number of combinations nCr
|
||||
00 0E Go: ENT R0 // n
|
||||
01 1E ENT R1 // r
|
||||
02 2C CLR R2
|
||||
03 2A Loop: ADD1 R2
|
||||
04 0D DEC R0
|
||||
05 1D DEC R1
|
||||
06 C3 JNZ Loop
|
||||
07 3C CLR R3 // for result
|
||||
08 3A ADD1 R3
|
||||
09 0A Next: ADD1 R0
|
||||
0A 1A ADD1 R1
|
||||
0B 50 R5 = R0
|
||||
0C 5D DEC R5
|
||||
0D 63 R6 = R3
|
||||
0E 46 Mult: R4 = R6
|
||||
0F 3A Add: ADD1 R3
|
||||
10 4D DEC R4
|
||||
11 CF JNZ Add
|
||||
12 5D DEC R5
|
||||
13 CE JNZ Mult
|
||||
14 61 Divide:R6 = R1
|
||||
15 5A ADD1 R5
|
||||
16 3D Sub: DEC R3
|
||||
17 9B JZ Exact
|
||||
18 6D DEC R6
|
||||
19 D6 JNZ Sub
|
||||
1A 94 JZ Divide
|
||||
1B 35 Exact: R3 = R5
|
||||
1C 2D DEC R2
|
||||
1D C9 JNZ Next
|
||||
1E 03 R0 = R3
|
||||
1F 80 JZ Go // Display result
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
convert(binomial(n,k),factorial);
|
||||
|
||||
binomial(5,3);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(Local) In[1]:= Binomial[5,3]
|
||||
(Local) Out[1]= 10
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
binomial( 5, 3); /* 10 */
|
||||
binomial(-5, 3); /* -35 */
|
||||
binomial( 5, -3); /* 0 */
|
||||
binomial(-5, -3); /* 0 */
|
||||
binomial( 3, 5); /* 0 */
|
||||
|
||||
binomial(x, 3); /* ((x - 2)*(x - 1)*x)/6 */
|
||||
|
||||
binomial(3, 1/2); /* binomial(3, 1/2) */
|
||||
makegamma(%); /* 32/(5*%pi) */
|
||||
|
||||
binomial(a, b); /* binomial(a, b) */
|
||||
makegamma(%); /* gamma(a + 1)/(gamma(-b + a + 1)*gamma(b + 1)) */
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
((dup 0 ==) 'succ (dup pred) '* linrec) :fact
|
||||
('dup dip dup ((fact) () (- fact) (fact * div)) spread) :binomial
|
||||
|
||||
5 3 binomial puts!
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
def binomialCoeff(n, k)
|
||||
result = 1
|
||||
for i in range(1, k)
|
||||
result = result * (n-i+1) / i
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
if main
|
||||
println binomialCoeff(5,3)
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
proc binomialCoeff(n, k: int): int =
|
||||
result = 1
|
||||
for i in 1..k:
|
||||
result = result * (n-i+1) div i
|
||||
|
||||
echo binomialCoeff(5, 3)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
let binomialCoeff n p =
|
||||
let p = if p < n -. p then p else n -. p in
|
||||
let rec cm res num denum =
|
||||
(* this method partially prevents overflow.
|
||||
* float type is choosen to have increased domain on 32-bits computer,
|
||||
* however algorithm ensures an integral result as long as it is possible
|
||||
*)
|
||||
if denum <= p then cm ((res *. num) /. denum) (num -. 1.) (denum +. 1.)
|
||||
else res in
|
||||
cm 1. n 1.
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#load "nums.cma";;
|
||||
open Num;;
|
||||
|
||||
let binomial n p =
|
||||
let m = min p (n - p) in
|
||||
if m < 0 then Int 0 else
|
||||
let rec a j v =
|
||||
if j = m then v
|
||||
else a (succ j) ((v */ (Int (n - j))) // (Int (succ j)))
|
||||
in a 0 (Int 1)
|
||||
;;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
open Num;;
|
||||
let rec binomial n k = if n = k then Int 1 else ((binomial (n-1) k) */ Int n) // Int (n-k)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
MODULE Binomial;
|
||||
IMPORT
|
||||
Out;
|
||||
|
||||
PROCEDURE For*(n,k: LONGINT): LONGINT;
|
||||
VAR
|
||||
i,m,r: LONGINT;
|
||||
|
||||
BEGIN
|
||||
ASSERT(n > k);
|
||||
r := 1;
|
||||
IF k > n DIV 2 THEN m := n - k ELSE m := k END;
|
||||
FOR i := 1 TO m DO
|
||||
r := r * (n - m + i) DIV i
|
||||
END;
|
||||
RETURN r
|
||||
END For;
|
||||
|
||||
BEGIN
|
||||
Out.Int(For(5,2),0);Out.Ln
|
||||
END Binomial.
|
||||
|
|
@ -0,0 +1 @@
|
|||
: binomial(n, k) | i | 1 k loop: i [ n i - 1+ * i / ] ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
declare
|
||||
fun {BinomialCoeff N K}
|
||||
{List.foldL {List.number 1 K 1}
|
||||
fun {$ Z I}
|
||||
Z * (N-I+1) div I
|
||||
end
|
||||
1}
|
||||
end
|
||||
in
|
||||
{Show {BinomialCoeff 5 3}}
|
||||
|
|
@ -0,0 +1 @@
|
|||
binomial(5,3)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
$n=5;
|
||||
$k=3;
|
||||
function factorial($val){
|
||||
for($f=2;$val-1>1;$f*=$val--);
|
||||
return $f;
|
||||
}
|
||||
$binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k));
|
||||
echo $binomial_coefficient;
|
||||
?>
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue