2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,11 +1,15 @@
|
|||
This programming task, is to calculate ANY binomial coefficient.
|
||||
|
||||
However, it has to be able to output <math>\binom{5}{3}</math>, which is 10.
|
||||
However, it has to be able to output <big><big><math>\binom{5}{3}</math></big></big>, which is '''10'''.
|
||||
|
||||
This formula is recommended:
|
||||
: <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>
|
||||
:: <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,15 @@
|
|||
: fact ( n -- n-factorial )
|
||||
dup 0 = [ drop 1 ] [ dup 1 - fact * ] if ;
|
||||
|
||||
: choose ( n k -- n-choose-k )
|
||||
2dup - fact swap fact * swap fact swap / ;
|
||||
|
||||
! 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 / ;
|
||||
|
|
@ -3,7 +3,7 @@ function binom(n,k)
|
|||
n == 1 && return 1
|
||||
k == 0 && return 1
|
||||
|
||||
binom(n-1,k-1) + binom (n-1,k) #recursive call
|
||||
(n * binom(n - 1, k - 1)) ÷ k #recursive call
|
||||
end
|
||||
|
||||
julia> binom(5,2)
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p }
|
||||
say 5 choose 3;
|
||||
say combinations(5, 3).elems;
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. $^p).Int }
|
||||
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p }
|
||||
say 5 choose 3;
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p) }
|
||||
|
|
@ -0,0 +1 @@
|
|||
sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
/*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. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
/*REXX program calculates binomial coefficients (also known as 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 !
|
||||
!: procedure; !=1; do j=2 to arg(1); !=!*j; end /*j*/; return !
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
/*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 !
|
||||
/*REXX program calculates binomial coefficients (also known as 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 /*j*/; return !
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
(1..60).to_a.combination(30).size #=> 118264581564861424
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
fn fact(n:u32) -> u64 {
|
||||
let mut f:u64 = n as u64;
|
||||
for i in 2..n {
|
||||
f *= i as u64;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
fn choose(n: u32, k: u32) -> u64 {
|
||||
let mut num:u64 = n as u64;
|
||||
for i in 1..k {
|
||||
num *= (n-i) as u64;
|
||||
}
|
||||
return num / fact(k);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", choose(5,3));
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
10 LET n=33: LET k=17: PRINT "Binomial ";n;",";k;" = ";
|
||||
20 LET r=1: LET d=n-k
|
||||
30 IF d>k THEN LET k=d: LET d=n-k
|
||||
40 IF n<=k THEN GO TO 90
|
||||
50 LET r=r*n
|
||||
60 LET n=n-1
|
||||
70 IF (d>1) AND (FN m(r,d)=0) THEN LET r=r/d: LET d=d-1: GO TO 70
|
||||
80 GO TO 40
|
||||
90 PRINT r
|
||||
100 DEF FN m(a,b)=a-INT (a/b)*b
|
||||
Loading…
Add table
Add a link
Reference in a new issue