Combinations and permutations: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) m Undo revision 301374 by SqrtNegInf (talk) |
→{{header|C++}}: new entry |
||
Line 3: | Line 3: | ||
{{wikipedia|Permutation}} |
{{wikipedia|Permutation}} |
||
;Task: |
;Task: |
||
Line 343: | Line 342: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
|||
{{libheader|Boost}} |
|||
Compiled with <code>g++-10 -O2 -g -Wall -fmessage-length=0 -std=c++03</code>, linked with <code>-lgmp</code> |
|||
<lang cpp>#include <boost/multiprecision/gmp.hpp> |
|||
#include <iostream> |
|||
using namespace boost::multiprecision; |
|||
mpz_int p(uint n, uint p) { |
|||
mpz_int r = 1; |
|||
mpz_int k = n - p; |
|||
while (n > k) |
|||
r *= n--; |
|||
return r; |
|||
} |
|||
mpz_int c(uint n, uint k) { |
|||
mpz_int r = p(n, k); |
|||
while (k) |
|||
r /= k--; |
|||
return r; |
|||
} |
|||
int main() { |
|||
for (uint i = 1u; i < 12u; i++) |
|||
std::cout << "P(12," << i << ") = " << p(12u, i) << std::endl; |
|||
for (uint i = 10u; i < 60u; i += 10u) |
|||
std::cout << "C(60," << i << ") = " << c(60u, i) << std::endl; |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>P(12,1) = 12 |
|||
P(12,2) = 132 |
|||
P(12,3) = 1320 |
|||
P(12,4) = 11880 |
|||
P(12,5) = 95040 |
|||
P(12,6) = 665280 |
|||
P(12,7) = 3991680 |
|||
P(12,8) = 19958400 |
|||
P(12,9) = 79833600 |
|||
P(12,10) = 239500800 |
|||
P(12,11) = 479001600 |
|||
C(60,10) = 75394027566 |
|||
C(60,20) = 4191844505805495 |
|||
C(60,30) = 118264581564861424 |
|||
C(60,40) = 4191844505805495 |
|||
C(60,50) = 75394027566</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |