langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -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,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,22 @@
|
|||
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 @@
|
|||
10
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
multi sub postfix:<!>(Int $a) {
|
||||
[*] 1..$a;
|
||||
}
|
||||
|
||||
sub binomialcoefficient($n, $k) {
|
||||
$n! / (($n - $k)! * $k!);
|
||||
}
|
||||
|
||||
say binomialcoefficient(5, 3);
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
Procedure Factor(n)
|
||||
Protected Result=1
|
||||
While n>0
|
||||
Result*n
|
||||
n-1
|
||||
Wend
|
||||
ProcedureReturn Result
|
||||
EndProcedure
|
||||
|
||||
Macro C(n,k)
|
||||
(Factor(n)/(Factor(k)*factor(n-k)))
|
||||
EndMacro
|
||||
|
||||
If OpenConsole()
|
||||
Print("Enter value n: "): n=Val(Input())
|
||||
Print("Enter value k: "): k=Val(Input())
|
||||
PrintN("C(n,k)= "+str(C(n,k)))
|
||||
|
||||
Print("Press ENTER to quit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
print "binomial (5,1) = "; binomial(5, 1)
|
||||
print "binomial (5,2) = "; binomial(5, 2)
|
||||
print "binomial (5,3) = "; binomial(5, 3)
|
||||
print "binomial (5,4) = "; binomial(5,4)
|
||||
print "binomial (5,5) = "; binomial(5,5)
|
||||
end
|
||||
|
||||
function binomial(n,k)
|
||||
coeff = 1
|
||||
for i = n - k + 1 to n
|
||||
coeff = coeff * i
|
||||
next i
|
||||
for i = 1 to k
|
||||
coeff = coeff / i
|
||||
next i
|
||||
binomial = coeff
|
||||
end function
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func integer: binomial (in integer: n, in var integer: k) is func
|
||||
result
|
||||
var integer: binomial is 0;
|
||||
local
|
||||
var integer: l is 0;
|
||||
begin
|
||||
if n >= k then
|
||||
if k > n - k then
|
||||
k := n - k; # Optimization
|
||||
end if;
|
||||
binomial := 1;
|
||||
l := 0;
|
||||
while l < k do
|
||||
binomial *:= n - l;
|
||||
incr(l);
|
||||
binomial := binomial div l;
|
||||
end while;
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln("binomial coefficient of (5, 3) is " <& binomial(5, 3));
|
||||
end func;
|
||||
|
|
@ -0,0 +1 @@
|
|||
nCr(n,k)
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#!/bin/sh
|
||||
n=5;
|
||||
k=3;
|
||||
calculate_factorial(){
|
||||
partial_factorial=1;
|
||||
for (( i=1; i<="$1"; i++ ))
|
||||
do
|
||||
factorial=$(expr $i \* $partial_factorial)
|
||||
partial_factorial=$factorial
|
||||
|
||||
done
|
||||
echo $factorial
|
||||
}
|
||||
|
||||
n_factorial=$(calculate_factorial $n)
|
||||
k_factorial=$(calculate_factorial $k)
|
||||
n_minus_k_factorial=$(calculate_factorial `expr $n - $k`)
|
||||
binomial_coefficient=$(expr $n_factorial \/ $k_factorial \* 1 \/ $n_minus_k_factorial )
|
||||
|
||||
echo "Binomial Coefficient ($n,$k) = $binomial_coefficient"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#import nat
|
||||
|
||||
choose = ~&ar^?\1! quotient^\~&ar product^/~&al ^|R/~& predecessor~~
|
||||
|
|
@ -0,0 +1 @@
|
|||
choose = quotient^/factorial@l product+ factorial^~/difference ~&r
|
||||
|
|
@ -0,0 +1 @@
|
|||
choose("n","k") = quotient(factorial "n",product factorial~~ (difference("n","k"),"k"))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#cast %nL
|
||||
|
||||
main = choose* <(5,3),(60,30)>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
code ChOut=8, CrLf=9, IntOut=11;
|
||||
|
||||
func Binomial(N, K);
|
||||
int N, K;
|
||||
int M, B, I;
|
||||
[M:= K;
|
||||
if K>N/2 the M:= N-K;
|
||||
B:=1;
|
||||
for I:= 1 to M do
|
||||
B:= B*(N-M+I)/I;
|
||||
return B;
|
||||
];
|
||||
|
||||
int N, K;
|
||||
[for N:= 0 to 9 do
|
||||
[for K:= 0 to 9 do
|
||||
[if N>=K then IntOut(0, Binomial(N,K));
|
||||
ChOut(0, 9\tab\);
|
||||
];
|
||||
CrLf(0);
|
||||
];
|
||||
] \Mr. Pascal's triangle!
|
||||
Loading…
Add table
Add a link
Reference in a new issue