Combinations and permutations: Difference between revisions

→‎{{header|Perl 6}}: rephrasing + not using the Gamma name
(Added Perl Implementation)
(→‎{{header|Perl 6}}: rephrasing + not using the Gamma name)
Line 311:
 
=={{header|Perl 6}}==
Perl 6 iscan't fairly limited withcompute veryarbitrary large floating point values., thus Itwe tendswill touse consider themlogarithms, as infiniteis whenoften theyneeded reachwhen <math>10^{300}</math>dealing orwith socombinations. This forcesWe'll us toalso use onlya logarithmsStirling andmethod to tweakapproximate them in order to show the exponential.<math>\ln(n!)</math>:
 
<math>\ln n! \approx
\frac{1}{2}\ln(2\pi n) + n\ln\left(\frac{n}{e} + \frac{1}{12 e n}\right)</math>
 
Notice that Perl6 can process arbitrary long integers, though. So it's not clear whether using floating points is useful in this case.
Line 318 ⟶ 321:
multi C($n, $k) { P($n, $k) / [*] 1 .. $k }
sub lgammalstirling(\zn) {
zn < 10 ?? lgammalstirling(zn+1) - log(zn+1) !!
.5*log(2*pi*zn)+ n*log(n/e+1/(12*e*n))
z*log(z/e+1/(12*e*z))-
log(z)
}
Line 332 ⟶ 333:
}
multi P($n, $k, :$float!) {
(lgammalstirling($n+1) - lgammalstirling($n -$k +1))
but Logarithm
}
multi C($n, $k, :$float!) {
(lgammalstirling($n+1) - lgammalstirling($n -$k +1) - lgammalstirling($k+1))
but Logarithm
}
1,935

edits