Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -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,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,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,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,10 @@
|
|||
function binom(n,k)
|
||||
n >= k || return 0 #short circuit base cases
|
||||
n == 1 && return 1
|
||||
k == 0 && return 1
|
||||
|
||||
binom(n-1,k-1) + binom (n-1,k) #recursive call
|
||||
end
|
||||
|
||||
julia> binom(5,2)
|
||||
10
|
||||
|
|
@ -0,0 +1 @@
|
|||
sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. $^p).Int }
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
function choose($n,$k) {
|
||||
if($k -le $n -and 0 -le $k) {
|
||||
$numerator = $denominator = 1
|
||||
0..($k-1) | foreach{
|
||||
$numerator *= ($n-$_)
|
||||
$denominator *= ($_ + 1)
|
||||
}
|
||||
$numerator/$denominator
|
||||
} else {
|
||||
"$k is greater than $n or lower than 0"
|
||||
}
|
||||
}
|
||||
choose 5 3
|
||||
choose 2 1
|
||||
choose 10 10
|
||||
choose 10 2
|
||||
choose 10 8
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
/*REXX program calculates binomial coefficients (aka, combinations). */
|
||||
numeric digits 100000 /*allow some gihugeic numbers. */
|
||||
parse arg n k . /*get N and K from the C.L.*/
|
||||
say 'combinations('n","k')=' comb(n,k) /*display the result to terminal.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────COMB subroutine─────────────────────*/
|
||||
comb: procedure; parse arg x,y; return fact(x) % (fact(x-y) * fact(y))
|
||||
/*──────────────────────────────────FACT subroutine─────────────────────*/
|
||||
fact: procedure; !=1; do j=2 to arg(1); !=!*j; end; return !
|
||||
/*REXX program calculates binomial coefficients (aka, combinations). */
|
||||
numeric digits 100000 /*be able to handle gihugeic numbers. */
|
||||
parse arg n k . /*obtain N and K from the C.L. */
|
||||
say 'combinations('n","k')=' comb(n,k) /*display the number of combinations. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y))
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
!: procedure; !=1; do j=2 to arg(1); !=!*j; end; return !
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
/*REXX program calculates binomial coefficients (aka, combinations). */
|
||||
numeric digits 100000 /*allow some gihugeic numbers. */
|
||||
parse arg n k . /*get N and K from the C.L.*/
|
||||
say 'combinations('n","k')=' comb(n,k) /*display the result to terminal.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────COMB subroutine─────────────────────*/
|
||||
comb: procedure; parse arg x,y; return pfact(x-y+1,x) % pfact(2,y)
|
||||
/*──────────────────────────────────PFACT subroutine────────────────────*/
|
||||
pfact: procedure; !=1; do j=arg(1) to arg(2); !=!*j; end; return !
|
||||
/*REXX program calculates binomial coefficients (aka, combinations). */
|
||||
numeric digits 100000 /*be able to handle gihugeic numbers. */
|
||||
parse arg n k . /*obtain N and K from the C.L. */
|
||||
say 'combinations('n","k')=' comb(n,k) /*display the number of combinations. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
comb: procedure; parse arg x,y; return pfact(x-y+1,x) % pfact(2,y)
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
pfact: procedure; !=1; do j=arg(1) to arg(2); !=!*j; end; return !
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
10 nCr 4
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
Function binomial(n,k)
|
||||
binomial = factorial(n)/(factorial(n-k)*factorial(k))
|
||||
End Function
|
||||
|
||||
Function factorial(n)
|
||||
If n = 0 Then
|
||||
factorial = 1
|
||||
Else
|
||||
For i = n To 1 Step -1
|
||||
If i = n Then
|
||||
factorial = n
|
||||
Else
|
||||
factorial = factorial * i
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
End Function
|
||||
|
||||
'calling the function
|
||||
WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3)
|
||||
WScript.StdOut.WriteLine
|
||||
Loading…
Add table
Add a link
Reference in a new issue