Evaluate binomial coefficients: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 355: | Line 355: | ||
Message box shows: |
Message box shows: |
||
<pre>10</pre> |
<pre>10</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |
||
Line 521: | Line 522: | ||
131282408400 |
131282408400 |
||
11923179284862717872</pre> |
11923179284862717872</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>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))); |
|||
} |
|||
</lang> |
|||
Implementation: |
|||
<lang cpp>int main() |
|||
{ |
|||
cout<<"The Binomial Coefficient of 5, and 3, is equal to: "<< binomialCoefficient(5,3); |
|||
cin.get(); |
|||
}</lang> |
|||
{{Out}} |
|||
<pre>The Binomial Coefficient of 5, and 3, is equal to: 10</pre> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 594: | Line 561: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
|||
<lang cpp>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))); |
|||
} |
|||
</lang> |
|||
Implementation: |
|||
<lang cpp>int main() |
|||
{ |
|||
cout<<"The Binomial Coefficient of 5, and 3, is equal to: "<< binomialCoefficient(5,3); |
|||
cin.get(); |
|||
}</lang> |
|||
{{Out}} |
|||
<pre>The Binomial Coefficient of 5, and 3, is equal to: 10</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 821: | Line 822: | ||
Binomial (33,17) = 1166803110 |
Binomial (33,17) = 1166803110 |
||
</pre> |
</pre> |
||
=={{header|F Sharp|F#}}== |
|||
<lang fsharp> |
|||
let choose n k = List.fold (fun s i -> s * (n-i+1)/i ) 1 [1..k] |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 839: | Line 845: | ||
: choose-fold ( n k -- n-choose-k ) |
: choose-fold ( n k -- n-choose-k ) |
||
2dup 1 + [a,b] product -rot - 1 [a,b] product / ; |
2dup 1 + [a,b] product -rot - 1 [a,b] product / ; |
||
</lang> |
|||
=={{header|F Sharp|F#}}== |
|||
<lang fsharp> |
|||
let choose n k = List.fold (fun s i -> s * (n-i+1)/i ) 1 [1..k] |
|||
</lang> |
</lang> |
||
Line 1,446: | Line 1,447: | ||
5 |
5 |
||
118264581564861424</pre> |
118264581564861424</pre> |
||
=={{header|Liberty BASIC}}== |
|||
<lang lb> |
|||
' [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 |
|||
</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 1,455: | Line 1,482: | ||
show choose 5 3 ; 10 |
show choose 5 3 ; 10 |
||
show choose 60 30 ; 1.18264581564861e+17</lang> |
show choose 60 30 ; 1.18264581564861e+17</lang> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<lang lua>function Binomial( n, k ) |
<lang lua>function Binomial( n, k ) |
||
Line 1,501: | Line 1,529: | ||
print( Binomial(100,50)) -- 1.0089134454556e+029 |
print( Binomial(100,50)) -- 1.0089134454556e+029 |
||
</lang> |
</lang> |
||
=={{header|Liberty BASIC}}== |
|||
<lang lb> |
|||
' [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 |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Line 1,658: | Line 1,659: | ||
10 |
10 |
||
</pre> |
</pre> |
||
=={{header|МК-61/52}}== |
|||
<lang>П1 <-> П0 ПП 22 П2 ИП1 ПП 22 П3 |
|||
ИП0 ИП1 - ПП 22 ИП3 * П3 ИП2 ИП3 |
|||
/ С/П ВП П0 1 ИП0 * L0 25 В/О</lang> |
|||
''Input'': ''n'' ^ ''k'' В/О С/П. |
|||
=={{header|MINIL}}== |
=={{header|MINIL}}== |
||
Line 1,708: | Line 1,702: | ||
which results from the definition of ncr in terms of factorials. |
which results from the definition of ncr in terms of factorials. |
||
=={{header|МК-61/52}}== |
|||
<lang>П1 <-> П0 ПП 22 П2 ИП1 ПП 22 П3 |
|||
ИП0 ИП1 - ПП 22 ИП3 * П3 ИП2 ИП3 |
|||
/ С/П ВП П0 1 ИП0 * L0 25 В/О</lang> |
|||
''Input'': ''n'' ^ ''k'' В/О С/П. |
|||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
Line 1,790: | Line 1,791: | ||
<lang OCaml>open Num;; |
<lang OCaml>open Num;; |
||
let rec binomial n k = if n = k then Int 1 else ((binomial (n-1) k) */ Int n) // Int (n-k)</lang> |
let rec binomial n k = if n = k then Int 1 else ((binomial (n-1) k) */ Int n) // Int (n-k)</lang> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 1,849: | Line 1,849: | ||
The Math::Pari module also has binomial, but it needs large amounts of added stack space for large arguments (this is due to using a very old version of the underlying Pari library). |
The Math::Pari module also has binomial, but it needs large amounts of added stack space for large arguments (this is due to using a very old version of the underlying Pari library). |
||
=={{header|Perl 6}}== |
|||
For a start, you can get the length of the corresponding list of combinations: |
|||
<lang perl6>say combinations(5, 3).elems;</lang> |
|||
{{out}} |
|||
<pre>10</pre> |
|||
This method is efficient, as Perl 6 will not actually compute each element of the list, since it actually uses an iterator with a defined <tt>count-only</tt> method. Such method performs computations in a way similar to the following infix operator: |
|||
<lang perl6>sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p } |
|||
say 5 choose 3;</lang> |
|||
A possible optimization would use a symmetry property of the binomial coefficient: |
|||
<lang perl6>sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p) }</lang> |
|||
One drawback of this method is that it returns a Rat, not an Int. So we actually may want to enforce the conversion: |
|||
<lang perl6>sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }</lang> |
|||
And ''this'' is exactly what the <tt>count-only</tt> method does. |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,172: | Line 2,152: | ||
(binomial 10 5) |
(binomial 10 5) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
For a start, you can get the length of the corresponding list of combinations: |
|||
<lang perl6>say combinations(5, 3).elems;</lang> |
|||
{{out}} |
|||
<pre>10</pre> |
|||
This method is efficient, as Perl 6 will not actually compute each element of the list, since it actually uses an iterator with a defined <tt>count-only</tt> method. Such method performs computations in a way similar to the following infix operator: |
|||
<lang perl6>sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p } |
|||
say 5 choose 3;</lang> |
|||
A possible optimization would use a symmetry property of the binomial coefficient: |
|||
<lang perl6>sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p) }</lang> |
|||
One drawback of this method is that it returns a Rat, not an Int. So we actually may want to enforce the conversion: |
|||
<lang perl6>sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }</lang> |
|||
And ''this'' is exactly what the <tt>count-only</tt> method does. |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,596: | Line 2,597: | ||
echo "Binomial Coefficient ($n,$k) = $binomial_coefficient"</lang> |
echo "Binomial Coefficient ($n,$k) = $binomial_coefficient"</lang> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 2,654: | Line 2,653: | ||
{{Out}} |
{{Out}} |
||
<pre>the binomial coefficient of 5 and 3 = 10</pre> |
<pre>the binomial coefficient of 5 and 3 = 10</pre> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |