Combinations and permutations: Difference between revisions
→{{header|11l}}
(47 intermediate revisions by 22 users not shown) | |||
Line 3:
{{wikipedia|Permutation}}
;Task:
Line 25 ⟶ 24:
{{Template:Combinations and permutations}}
<br><br>
=={{header|11l}}==
{{trans|C++}}
<syntaxhighlight lang="11l">F perm(=n, p)
BigInt r = 1
V k = n - p
L n > k
r *= n--
R r
F comb(n, =k)
V r = perm(n, k)
L k > 0
r I/= k--
R r
L(i) 1..11
print(‘P(12,’i‘) = ’perm(12, i))
L(i) (10.<60).step(10)
print(‘C(60,’i‘) = ’comb(60, i))</syntaxhighlight>
{{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|Ada}}==
Ada 2022 has Big_Integers, also we would probably use generic functions - but that's hard to show in a single file.
<syntaxhighlight lang="ada">
pragma Ada_2022;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
use Ada.Numerics.Big_Numbers.Big_Integers;
with Interfaces; use Interfaces;
procedure Combs_Perms is
function U64_Perm (N, K : Unsigned_64) return Unsigned_64 is
P : Unsigned_64 := 1;
begin
if K = 0 then
P := 0;
else
for I in 0 .. K - 1 loop
P := P * (N - I);
end loop;
end if;
return P;
end U64_Perm;
function Big_Perm (N, K : Natural) return Big_Natural is
P : Big_Natural := 1;
begin
if K = 0 then
P := 0;
else
for I in 0 .. K - 1 loop
P := P * To_Big_Integer (N - I);
end loop;
end if;
return P;
end Big_Perm;
function U64_Comb (N, K : Unsigned_64) return Unsigned_64 is
Adj_K : constant Unsigned_64 := (if N - K < K then N - K else K);
C : Unsigned_64 := U64_Perm (N, Adj_K);
begin
if K = 0 then
C := 0;
else
for I in reverse 1 .. Adj_K loop
C := C / I;
end loop;
end if;
return C;
end U64_Comb;
function Big_Comb (N, K : Natural) return Big_Natural is
Adj_K : constant Natural := (if N - K < K then N - K else K);
C : Big_Natural := Big_Perm (N, Adj_K);
begin
for I in reverse 1 .. Adj_K loop
C := C / To_Big_Integer (I);
end loop;
return C;
end Big_Comb;
begin
Put_Line ("P(1, 0) =" & U64_Perm (1, 0)'Image);
Put_Line ("P(12, 4) =" & U64_Perm (12, 4)'Image);
Put_Line ("P(60, 20) =" & U64_Perm (60, 20)'Image);
Put_Line ("P(105, 103) =" & Big_Perm (105, 103)'Image);
Put_Line ("P(15000, 333) =" & Big_Perm (10000, 333)'Image);
Put_Line ("C(10, 5) =" & U64_Comb (10, 5)'Image);
Put_Line ("C(60, 30) =" & Big_Comb (60, 30)'Image);
Put_Line ("C(900, 674) =" & Big_Comb (900, 674)'Image);
end Combs_Perms;
</syntaxhighlight>
{{out}}
<pre>
P(1, 0) = 0
P(12, 4) = 11880
P(60, 20) = 1808792028959211520
P(105, 103) = 540698379120145450252050652900164824860323053887451289572088318286613265954952576663492268263404120169888199467436014828996936453906718408048640000000000000000000000000
P(15000, 333) = 3734632779825648626849478204217851175491031852573933200482258207588048438187890916059251484636506271051265834404767122580751173394985841384750846042214481658210687940637400976395676712758506534427611745860428779464238199671888490205087478284041341740481214200754003412080717927644948303742303217778756499322250874934482078713617010428749333465186089669624344010164333863671606638752312981517247538583321540255309837467575709099947985095703433121711204915009751758378839557821815211950252111673682998724830733294759824988032874155681853213162745288956061564026603816655765483177328085099709270712978486637711013862688274863138979103405877374342534841337812225037918093228818727609066843343063448062504973631670561421262499196336524811809011228722269099771902324209604494466003586741308023495564221843755524784021722020833255027012307883615611523716891547845775441485354168687580879756175578658189002755076470466059881166684817696505920834137318475281619250730364346027906505163737946409176743772248798012981461588038416291189183602392530371700797564272591564324880128780642945107817817148785181713276188934924102752418421382343547688708913308696047351312049027540308819760102775940016773345719986101919685659117157000736526082591575378358047866880000000000000000000000000000000000000000000000000000000000000000000000000000000000000
C(10, 5) = 252
C(60, 30) = 118264581564861424
C(900, 674) = 574717622813743458961613396879207645492072766667532001303398319071426357199738020963741184130318716754384770618027834663186286577340376549182421952347640973972155470158739236146281280772731378323732881115811005924314000
</pre>
=={{header|ALGOL 68}}==
Line 30 ⟶ 155:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude_combinations_and_permutations.a68'''<
COMMENT REQUIRED by "prelude_combinations_and_permutations.a68" CO
Line 120 ⟶ 245:
END COMMENT
SKIP</
# -*- coding: utf-8 -*- #
Line 169 ⟶ 294:
printf(($"("g(0)" C "g(0)") = "g(0,1)$, r, first, r C first, $", "$));
printf(($"("g(0)" C "g(0)") = "g(0,1)$, r, second, r C second, $l$))
OD</
<pre>
A sample of Permutations from 1 to 12:
Line 219 ⟶ 344:
(1000 C 998) = 499500.0, (1000 C 969) = 76023224077705100000000000000000000000000000000000000000000.0
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">printPermutations: function [s,b][
print [
"P(" ++ (to :string s) ++ ", " ++ (to :string b) ++ ") = "
permutate.count.by:b @1..s
]
]
printCombinations: function [s,b][
print [
"C(" ++ (to :string s) ++ ", " ++ (to :string b) ++ ") = "
combine.count.by:b @1..s
]
]
printPermutations 4 2
printPermutations 5 3
printPermutations 6 4
printPermutations 7 5
printPermutations 8 6
printPermutations 9 7
printPermutations 10 8
printPermutations 11 9
printPermutations 12 10
printCombinations 10 8
printCombinations 20 18
printCombinations 30 28
printCombinations 40 38
printCombinations 50 48
printCombinations 60 58
printPermutations 340 230
printPermutations 12503 150
printCombinations 180 40
printCombinations 970 730
</syntaxhighlight>
{{out}}
<pre>P(4, 2) = 12
P(5, 3) = 60
P(6, 4) = 360
P(7, 5) = 2520
P(8, 6) = 20160
P(9, 7) = 181440
P(10, 8) = 1814400
P(11, 9) = 19958400
P(12, 10) = 239500800
C(10, 8) = 45
C(20, 18) = 190
C(30, 28) = 435
C(40, 38) = 780
C(50, 48) = 1225
C(60, 58) = 1770
P(340, 230) = 321163468667018390278490855166116943643915115883733627394541858241858716874364895663708654024956544433526906929220370657178037367849691887585605027786348781323683175128970012088045935740649918420588744620257725216845699632806302776510600871474726786041782200035955935929612857643030896557053167285732409630111804341546261862735713347179073784452257244336775041366281305006608295780372754879670614948719662927123558774623452956076264437724405141046975318837351290035912604435611648000000000000000000000000000000000000000000000000000000000
P(12503, 150) = 145349029342353586631193854127379263889050707122840603258866143765734805000836318095430535878283222399986691383724192776832431401841845807936456562635873483263903741650446537273646795588914347285406358840689987529465482210964310466322364415189531610901593861947474126246323721536314280740814336769614384426370056684232165925069433834623283158338231715677812329656785966269970444585319639178491261742879187758508103438009278190651455732354378754778290406101258502700133885808094777181331499601845451634234055547273229226095279158651885556410705696845808314349918299242931159040000000000000000000000000000000000000000
C(180, 40) = 18290112128130495302949953479795255876590
C(970, 730) = 1460703798049835817371501066063364773159305309662093793682805325059313312798468600051903640905890411826065678898559485932184945661085268182815842989416642105002033447941384758262043195945021701492825484266010877073654424416642736902160</pre>
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat"> ( C
= n k coef
. !arg:(?n,?k)
Line 235 ⟶ 421:
& !coef
)
& ( compileBinomialFunctionThatDoesFloatingPointCalculations
=
. new
$ ( UFP
,
' ( (s.n) (s.k)
. "**************************************************************
*** Notice the difference between the following four lines ***
*** of code and the much shorter (!n+-1*!k:<!k:?k|) in ***
*** function C above. UFP grammar is simpler than usual ***
*** Bracmat grammar. UFP code is therefore less terse. ***
**************************************************************"
& !n+-1*!k:?n-k
& ( !n-k:<!k&!n-k:?k
|
)
& 1:?coef
& whl
' ( !k:>0
& !coef*!n*!k^-1:?coef
& !k+-1:?k
& !n+-1:?n
)
& !coef
)
)
)
& compileBinomialFunctionThatDoesFloatingPointCalculations$
: ?binom
& ( P
= n k result
Line 247 ⟶ 462:
& !result
)
& ( compilePermutationFunctionThatDoesFloatingPointCalculations
=
. new
$ ( UFP
,
' ( (s.n) (s.k)
. !n+-1*!k:?k
& 1:?result
& whl
' ( !n:>!k
& !n*!result:?result
& !n+-1:?n
)
& !result
)
)
)
& compilePermutationFunctionThatDoesFloatingPointCalculations$
: ?permu
& 0:?i
& whl
Line 257 ⟶ 491:
' ( 10+!i:~>60:?i
& div$(!i.3):?k
& out$(!i
& out$(!i Cf !k "=" (binom..go)$(!i,!k))
)
& ( displayBig
Line 269 ⟶ 504:
' ( !is:%?i ?is
& div$(!i.3):?k
& out
$ ( str
$ (!i " Pn " !k " = " displayBig$(P$(!i,!k)))
)
& out
$ ( str
$ (!i " Pf " !k " = " (permu..go)$(!i,!k))
)
)
& 0:?i
Line 275 ⟶ 517:
' ( 100+!i:~>1000:?i
& div$(!i.3):?k
& out
$ ( str
$ (!i " Cn " !k " = " displayBig$(C$(!i,!k)))
)
& out
$ ( str
$ (!i " Cf " !k " = " (binom..go)$(!i,!k))
)
)
& all done;</syntaxhighlight>
Output:
<pre>1 P 0 = 1
Line 291 ⟶ 540:
11 P 3 = 990
12 P 4 = 11880
10
10 Cf 3 = 1.1999999999999999E+02
20 Cf 6 = 3.8759999999999993E+04
30 Cf 10 = 3.0045014999999989E+07
40 Cf 13 = 1.2033222880000000E+10
50 Cn 16 = 4923689695575
50 Cf 16 = 4.9236896955750000E+12
60 Cn 20 = 4191844505805495
60 Cf 20 = 4.1918445058054930E+15
5 Pn 1 = 5
5 Pf 1 = 5.0000000000000000E+00
50 Pn 16 = 103017324974226408345600000
50 Pf 16 = 1.0301732497422640E+26
500
500 Pf 166 = INF
1000 Pf 333 = INF
5000 Pf 1666 = INF
15000 Pn 5000 = 96498539887274939220148588059312959807922816886808... (20420 more digits)
15000 Pf 5000 = INF
100 Cn 33 = 294692427022540894366527900
100 Cf 33 = 2.9469242702254079E+26
200 Cn 66 = 72697525451692783415270666511927389767550269141935... (4 more digits)
200 Cf 66 = 7.2697525451692816E+53
300 Cn 100 = 41582514632585647447833835263264055802804660057436... (32 more digits)
300 Cf 100 = 4.1582514632585569E+81
400 Cn 133 = 12579486841821087021333484756519650044917494358375... (60 more digits)
400 Cf 133 = 1.2579486841821091E+109
500 Cn 166 = 39260283861944227552204083450723314281973490135301... (87 more digits)
500 Cf 166 = 3.9260283861944195E+136
600 Cn 200 = 25060177832214028050056167705132288352025510250879... (115 more digits)
600 Cf 200 = 2.5060177832213946E+164
700 Cn 233 = 81032035633395999047404536440311382329449203119421... (142 more digits)
700 Cf 233 = 8.1032035633395859E+191
800 Cn 266 = 26456233626836270342888292995561242550915240450150... (170 more digits)
800 Cf 266 = 2.6456233626836295E+219
900 Cn 300 = 17433563732964466429607307650857183476303419689548... (198 more digits)
900 Cf 300 = 1.7433563732964451E+247
1000 Cn 333 = 57761345531476516697774863235496017223394580195002... (225 more digits)
1000 Cf 333 = 5.7761345531476355E+274
{!} all done</pre>
=={{header|C}}==
Using big integers. GMP in fact has a factorial function which is quite possibly more efficient, though using it would make code longer.
<
void perm(mpz_t out, int n, int k)
Line 342 ⟶ 614:
gmp_printf("C(1000,969) = %Zd\n", x);
return 0;
}</
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Numerics;
class CombinationsAndPermutations
{
public static void Main(string[] args)
{
Console.WriteLine(double.MaxValue);
Console.WriteLine("A sample of permutations from 1 to 12 with exact Integer arithmetic:");
for (int n = 1; n <= 12; n++)
{
int k = n / 2;
Console.WriteLine($"{n} P {k} = {Permutation(n, k)}");
}
Console.WriteLine();
Console.WriteLine("A sample of combinations from 10 to 60 with exact Integer arithmetic:");
for (int n = 10; n <= 60; n += 5)
{
int k = n / 2;
Console.WriteLine($"{n} C {k} = {Combination(n, k)}");
}
Console.WriteLine();
Console.WriteLine("A sample of permutations from 5 to 15000 displayed in floating point arithmetic:");
Console.WriteLine($"{5} P {2} = {Display(Permutation(5, 2), 50)}");
for (int n = 1000; n <= 15000; n += 1000)
{
int k = n / 2;
Console.WriteLine($"{n} P {k} = {Display(Permutation(n, k), 50)}");
}
Console.WriteLine();
Console.WriteLine("A sample of combinations from 100 to 1000 displayed in floating point arithmetic:");
for (int n = 100; n <= 1000; n += 100)
{
int k = n / 2;
Console.WriteLine($"{n} C {k} = {Display(Combination(n, k), 50)}");
}
}
private static string Display(BigInteger val, int precision)
{
string s = val.ToString();
// Ensure that we don't try to take a substring longer than what's available.
int actualPrecision = Math.Min(precision, s.Length - 1); // Adjusted to ensure it doesn't exceed string length
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (s.Length > 1) // Check if the string has more than one character
{
sb.Append(s.Substring(0, 1));
sb.Append(".");
sb.Append(s.Substring(1, actualPrecision-1));
}
else
{
// If the string is only one digit, no need to insert a decimal point.
sb.Append(s);
}
sb.Append(" * 10^");
sb.Append(s.Length - 1);
return sb.ToString();
}
public static BigInteger Combination(int n, int k)
{
// Select value with smallest intermediate results
// combination(n, k) = combination(n, n-k)
if (n - k < k)
{
k = n - k;
}
BigInteger result = Permutation(n, k);
while (k > 0)
{
result = result / k;
k--;
}
return result;
}
public static BigInteger Permutation(int n, int k)
{
BigInteger result = BigInteger.One;
for (int i = n; i >= n - k + 1; i--)
{
result = result * i;
}
return result;
}
}
</syntaxhighlight>
{{out}}
<pre>
1.79769313486232E+308
A sample of permutations from 1 to 12 with exact Integer arithmetic:
1 P 0 = 1
2 P 1 = 2
3 P 1 = 3
4 P 2 = 12
5 P 2 = 20
6 P 3 = 120
7 P 3 = 210
8 P 4 = 1680
9 P 4 = 3024
10 P 5 = 30240
11 P 5 = 55440
12 P 6 = 665280
A sample of combinations from 10 to 60 with exact Integer arithmetic:
10 C 5 = 252
15 C 7 = 6435
20 C 10 = 184756
25 C 12 = 5200300
30 C 15 = 155117520
35 C 17 = 4537567650
40 C 20 = 137846528820
45 C 22 = 4116715363800
50 C 25 = 126410606437752
55 C 27 = 3824345300380220
60 C 30 = 118264581564861424
A sample of permutations from 5 to 15000 displayed in floating point arithmetic:
5 P 2 = 2. * 10^1
1000 P 500 = 3.2978863640988537122024252070116261706996485697981 * 10^1433
2000 P 1000 = 8.2415012140674255266380217928953202425839550211348 * 10^3167
3000 P 1500 = 8.6229457673788561572282325050469704818001698192368 * 10^5015
4000 P 2000 = 5.5146268042637929575202252487087217092097210095831 * 10^6937
5000 P 2500 = 2.5959899179479570425022364594725223975158226787173 * 10^8914
6000 P 3000 = 6.4684674799045492726351265543133876130483115297807 * 10^10934
7000 P 3500 = 3.6978393541988590953223392044469547113117500019066 * 10^12991
8000 P 4000 = 2.8347416494109128583587900207169062803923698647730 * 10^15079
9000 P 4500 = 3.5613559022062915735922606191272180393638776777367 * 10^17194
10000 P 5000 = 6.7310091721588924046678115055013992269082753633190 * 10^19333
11000 P 5500 = 7.1714299723894354619599359891785521535675525394493 * 10^21494
12000 P 6000 = 4.4778633072110971517945389774618813158941392935141 * 10^23675
13000 P 6500 = 3.6571405647713639990172807053439865891697112165983 * 10^25874
14000 P 7000 = 1.5680448683572888099973548116924953997328821172933 * 10^28090
15000 P 7500 = 2.2454775022523692436484435950526532351441855443096 * 10^30321
A sample of combinations from 100 to 1000 displayed in floating point arithmetic:
100 C 50 = 1.0089134454556419333481249725 * 10^29
200 C 100 = 9.0548514656103281165404177077484163874504589675413 * 10^58
300 C 150 = 9.3759702772827452793193754439064084879232655700081 * 10^88
400 C 200 = 1.0295250013541443297297588032040198675721092538107 * 10^119
500 C 250 = 1.1674431578827768292093473476217661965923008118031 * 10^149
600 C 300 = 1.3510794199619426851447487797850453039723394544919 * 10^179
700 C 350 = 1.5857433585316795487607022764754299250587342835720 * 10^209
800 C 400 = 1.8804244186835312700958607615195351332156581822914 * 10^239
900 C 450 = 2.2474718820660159573188913903771345843514101350359 * 10^269
1000 C 500 = 2.7028824094543656951561469362597527549615200844654 * 10^299
</pre>
=={{header|C++}}==
{{libheader|Boost}}
Compiled with <code>g++-10 -Wall -std=c++03</code>, linked with <code>-lgmp</code>
<syntaxhighlight 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;
}</syntaxhighlight>
{{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}}==
<
(cond ((or (< n k) (< k 0) (< n 0)) 0)
((= k 0) 1)
Line 362 ⟶ 844:
(a m (* a m)))
((= i k) a)))))
</syntaxhighlight>
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">require "big"
include Math
struct Int
def permutation(k)
(self-k+1..self).product(1.to_big_i)
end
def combination(k)
self.permutation(k) // (1..k).product(1.to_big_i)
end
def big_permutation(k)
exp(lgamma_plus(self) - lgamma_plus(self-k))
end
def big_combination(k)
exp( lgamma_plus(self) - lgamma_plus(self - k) - lgamma_plus(k))
end
private def lgamma_plus(n)
lgamma(n+1) #lgamma is the natural log of gamma
end
end
p 12.permutation(9) #=> 79833600
p 12.big_permutation(9) #=> 79833600.00000021
p 60.combination(53) #=> 386206920
p 145.big_permutation(133) #=> 1.6801459655817956e+243
p 900.big_combination(450) #=> 2.247471882064647e+269
p 1000.big_combination(969) #=> 7.602322407770517e+58
p 15000.big_permutation(73) #=> 6.004137561717704e+304
#That's about the maximum of Float:
p 15000.big_permutation(74) #=> Infinity
#Fixnum has no maximum:
p 15000.permutation(74) #=> 896237613852967826239917238565433149353074416025197784301593335243699358040738127950872384197159884905490054194835376498534786047382445592358843238688903318467070575184552953997615178973027752714539513893159815472948987921587671399790410958903188816684444202526779550201576117111844818124800000000000000000000
</syntaxhighlight>
{{out}}
<pre>
79833600
79833600.00000021
386206920
1.6801459655817956e+243
2.247471882064647e+269
7.602322407770517e+58
6.004137561717704e+304
Infinity
896237613852967826239917238565433149353074416025197784301593335243699358040738127950872384197159884905490054194835376498534786047382445592358843238688903318467070575184552953997615178973027752714539513893159815472948987921587671399790410958903188816684444202526779550201576117111844818124800000000000000000000
</pre>
=={{header|D}}==
{{trans|Ruby}}
<
std.bigint, std.conv;
Line 391 ⟶ 928:
15_000.bigPermutation(1185).writeln;
writefln("%(%s\\\n%)", 15_000.permutation(74).text.chunks(50));
}</
{{out}}
<pre>79833600
Line 408 ⟶ 945:
84444202526779550201576117111844818124800000000000\
000000000</pre>
=={{header|EasyLang}}==
{{trans|Phix}}
<syntaxhighlight>
func perm x y .
z = 1
for i = x - y + 1 to x
z *= i
.
return z
.
func fact x .
z = 1
for i = 2 to x
z *= i
.
return z
.
func comb x y .
if x - y < y
y = x - y
.
return perm x y / fact y
.
#
e = 2.7182818284590452354
func log n .
return log10 n / log10 e
.
func lstirling n .
if n < 10
return lstirling (n + 1) - log (n + 1)
.
return 0.5 * log (2 * pi * n) + n * log (n / e + 1 / (12 * e * n))
.
func$ tolog v .
h = v div log 10
return pow e (v - h * log 10) & "e" & h
.
func$ permf n k .
return tolog (lstirling n - lstirling (n - k))
.
func$ combf n k .
return tolog (lstirling n - lstirling (n - k) - lstirling k)
.
print "=> Exact results:"
for n = 1 to 12
p = n div 3
print "P(" & n & "," & p & ")=" & perm n p
.
#
# double has 53 bits for integer
#
for n = 10 step 10 to 50
p = n div 3
print "C(" & n & "," & p & ")=" & comb n p
.
#
print ""
print "=> Floating point approximations:"
for n in [ 5 50 500 1000 5000 15000 ]
p = n div 3
print "P(" & n & "," & p & ")=" & permf n p
.
for n = 100 step 100 to 1000
p = n div 3
print "C(" & n & "," & p & ")=" & combf n p
.
</syntaxhighlight>
=={{header|EchoLisp}}==
<
;; rename native functions according to task
(define-syntax-rule (Cnk n k) (Cnp n k))
Line 431 ⟶ 1,038:
→ 1029024198692120734765388598788124551227594950478035495578451793852872815678512303375588360
1398831219998720000000000000
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Erlang}}
<
def perm(n, k), do: product(n - k + 1 .. n)
Line 472 ⟶ 1,079:
end
Combinations_permutations.test</
{{out}}
Line 522 ⟶ 1,129:
{{trans|Haskell}}
<
-module(combinations_permutations).
Line 565 ⟶ 1,172:
io_lib:format("~s... (~p more digits)", [Shown, length(Hidden)])
end.
</syntaxhighlight>
Output:
Line 611 ⟶ 1,218:
comb(1000, 333) = 5776134553147651669777486323549601722339... (235 more digits)
</pre>
=={{header|Factor}}==
As with Racket, these operations are built in and Factor has unlimited integers.
<syntaxhighlight lang="factor">USING: math.combinatorics prettyprint ;
1000 10 nCk . ! 263409560461970212832400
1000 10 nPk . ! 955860613004397508326213120000</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="freebasic">Function PermBig(x As Long, y As Long) As ULongint
Dim As Long i
Dim As Longint z = 1
For i = x - y + 1 To x
z = z * i
Next i
Return (z)
End Function
Function FactBig(x As Long) As ULongint
Dim As Long i
Dim As Longint z = 1
For i = 2 To x
z = z * i
Next i
Return (z)
End Function
Function CombBig(Byval x As Long, Byval y As Long) As Double
If y > x Then
Return (0)
Elseif x = y Then
Return (1)
Else
If x - y < y Then y = x - y
Return (PermBig(x, y) / FactBig(y))
End If
End Function
Dim As Long i, j
Print "-- Long Integer - Permutations - from 1 to 12"
For i = 1 To 12
For j = 1 To i
Print "P(" & i & "," & j & ")=" & Str(PermBig(i, j)) & " ";
Next j
Print ""
Next i
Print Chr(10) & "-- Float integer - Combinations from 10 to 60"
For i = 10 To 60 Step 10
For j = 1 To i Step i \ 5
Print "C(" & i & "," & j & ")=" & Str(CombBig(i, j)) & " ";
Next j
Print ""
Next i
Print Chr(10) & "-- Float integer - Permutations from 5000 to 15000"
For i = 5000 To 15000 Step 5000
For j = 10 To 50 Step 20
Print "P(" & i & "," & j & ")=" & Str(PermBig(i, j)) & " ";
Next j
Print ""
Next i
Print Chr(10) & "-- Float integer - Combinations from 200 to 1000"
For i = 200 To 1000 Step 200
For j = 20 To 100 Step 20
Print "C(" & i & "," & j & ")=" & Str(CombBig(i, j)) & " ";
Next j
Print ""
Next i
Sleep</syntaxhighlight>
=={{header|Go}}==
Go has arbitrary-length maths in the standard math/big library; no need for floating-point approximations at this level.
<syntaxhighlight lang="go">
package main
Line 679 ⟶ 1,359:
return c
}
</syntaxhighlight>
Output:
<pre>A sample of permutations from 1 to 12:
Line 726 ⟶ 1,406:
=={{header|Haskell}}==
The Haskell Integer type supports arbitrary precision so floating point approximation is not needed.
<
perm n k = product [n-k+1..n]
Line 764 ⟶ 1,444:
mapM_ showComb [(n, n `div` 3) | n <- [100,200..1000] ]
</syntaxhighlight>
{{out}}
<pre style="font-size:80%">A sample of permutations from 1 to 12:
Line 815 ⟶ 1,495:
The sample here gives a few representative values to shorten the output.
<
write("P(4,2) = ",P(4,2))
write("P(8,2) = ",P(8,2))
Line 839 ⟶ 1,519:
every (p:=1) *:= (n-k+1) to n
return p
end</
Output:
Line 868 ⟶ 1,548:
Implementation:
<
P=: (%&!&x:~ * <:)"0</
! is a primitive, but we will give it a name (<code>C</code>) for this task.
Line 875 ⟶ 1,555:
Example use (P is permutations, C is combinations):
<
┌──┬─────────────────────────────────────────────────────────────┐
│P │1 2 3 4 5 6 7 8 9 10 11 12│
Line 921 ⟶ 1,601:
2.24185e96
700 C 800
3.41114e129</
=={{header|Java}}==
The second part of this task implies that the computations are performed in floating point arithmetic. However, the maximum value of a double in Java is 1.7976931348623157E308. Hence, larger computations would overflow. As a result, this task was interpreted so that “using” means “displayed”.
<syntaxhighlight lang="java">
import java.math.BigInteger;
public class CombinationsAndPermutations {
public static void main(String[] args) {
System.out.println(Double.MAX_VALUE);
System.out.println("A sample of permutations from 1 to 12 with exact Integer arithmetic:");
for ( int n = 1 ; n <= 12 ; n++ ) {
int k = n / 2;
System.out.printf("%d P %d = %s%n", n, k, permutation(n, k));
}
System.out.println();
System.out.println("A sample of combinations from 10 to 60 with exact Integer arithmetic:");
for ( int n = 10 ; n <= 60 ; n += 5 ) {
int k = n / 2;
System.out.printf("%d C %d = %s%n", n, k, combination(n, k));
}
System.out.println();
System.out.println("A sample of permutations from 5 to 15000 displayed in floating point arithmetic:");
System.out.printf("%d P %d = %s%n", 5, 2, display(permutation(5, 2), 50));
for ( int n = 1000 ; n <= 15000 ; n += 1000 ) {
int k = n / 2;
System.out.printf("%d P %d = %s%n", n, k, display(permutation(n, k), 50));
}
System.out.println();
System.out.println("A sample of combinations from 100 to 1000 displayed in floating point arithmetic:");
for ( int n = 100 ; n <= 1000 ; n += 100 ) {
int k = n / 2;
System.out.printf("%d C %d = %s%n", n, k, display(combination(n, k), 50));
}
}
private static String display(BigInteger val, int precision) {
String s = val.toString();
precision = Math.min(precision, s.length());
StringBuilder sb = new StringBuilder();
sb.append(s.substring(0, 1));
sb.append(".");
sb.append(s.substring(1, precision));
sb.append(" * 10^");
sb.append(s.length()-1);
return sb.toString();
}
public static BigInteger combination(int n, int k) {
// Select value with smallest intermediate results
// combination(n, k) = combination(n, n-k)
if ( n-k < k ) {
k = n-k;
}
BigInteger result = permutation(n, k);
while ( k > 0 ) {
result = result.divide(BigInteger.valueOf(k));
k--;
}
return result;
}
public static BigInteger permutation(int n, int k) {
BigInteger result = BigInteger.ONE;
for ( int i = n ; i >= n-k+1 ; i-- ) {
result = result.multiply(BigInteger.valueOf(i));
}
return result;
}
}
</syntaxhighlight>
{{out}}
<pre>
1.7976931348623157E308
A sample of permutations from 1 to 12 with exact Integer arithmetic:
1 P 0 = 1
2 P 1 = 2
3 P 1 = 3
4 P 2 = 12
5 P 2 = 20
6 P 3 = 120
7 P 3 = 210
8 P 4 = 1680
9 P 4 = 3024
10 P 5 = 30240
11 P 5 = 55440
12 P 6 = 665280
A sample of combinations from 10 to 60 with exact Integer arithmetic:
10 C 5 = 252
15 C 7 = 6435
20 C 10 = 184756
25 C 12 = 5200300
30 C 15 = 155117520
35 C 17 = 4537567650
40 C 20 = 137846528820
45 C 22 = 4116715363800
50 C 25 = 126410606437752
55 C 27 = 3824345300380220
60 C 30 = 118264581564861424
A sample of permutations from 5 to 15000 displayed in floating point arithmetic:
5 P 2 = 2.0 * 10^1
1000 P 500 = 3.2978863640988537122024252070116261706996485697981 * 10^1433
2000 P 1000 = 8.2415012140674255266380217928953202425839550211348 * 10^3167
3000 P 1500 = 8.6229457673788561572282325050469704818001698192368 * 10^5015
4000 P 2000 = 5.5146268042637929575202252487087217092097210095831 * 10^6937
5000 P 2500 = 2.5959899179479570425022364594725223975158226787173 * 10^8914
6000 P 3000 = 6.4684674799045492726351265543133876130483115297807 * 10^10934
7000 P 3500 = 3.6978393541988590953223392044469547113117500019066 * 10^12991
8000 P 4000 = 2.8347416494109128583587900207169062803923698647730 * 10^15079
9000 P 4500 = 3.5613559022062915735922606191272180393638776777367 * 10^17194
10000 P 5000 = 6.7310091721588924046678115055013992269082753633190 * 10^19333
11000 P 5500 = 7.1714299723894354619599359891785521535675525394493 * 10^21494
12000 P 6000 = 4.4778633072110971517945389774618813158941392935141 * 10^23675
13000 P 6500 = 3.6571405647713639990172807053439865891697112165983 * 10^25874
14000 P 7000 = 1.5680448683572888099973548116924953997328821172933 * 10^28090
15000 P 7500 = 2.2454775022523692436484435950526532351441855443096 * 10^30321
A sample of combinations from 100 to 1000 displayed in floating point arithmetic:
100 C 50 = 1.00891344545564193334812497256 * 10^29
200 C 100 = 9.0548514656103281165404177077484163874504589675413 * 10^58
300 C 150 = 9.3759702772827452793193754439064084879232655700081 * 10^88
400 C 200 = 1.0295250013541443297297588032040198675721092538107 * 10^119
500 C 250 = 1.1674431578827768292093473476217661965923008118031 * 10^149
600 C 300 = 1.3510794199619426851447487797850453039723394544919 * 10^179
700 C 350 = 1.5857433585316795487607022764754299250587342835720 * 10^209
800 C 400 = 1.8804244186835312700958607615195351332156581822914 * 10^239
900 C 450 = 2.2474718820660159573188913903771345843514101350359 * 10^269
1000 C 500 = 2.7028824094543656951561469362597527549615200844654 * 10^299
</pre>
=={{header|jq}}==
and only supports tgamma (true gamma), and thus beyond about 1e308 all accuracy is lost.
On the other hand, gojq, the Go implementation, supports
unbounded-precision integer arithmetic,
and the `permutation` and `combination` functions below are
implemented so as to preserve precision.
<syntaxhighlight lang="jq">
def permutation(k): . as $n
| reduce range($n-k+1; 1+$n) as $i (1; . * $i);
Line 944 ⟶ 1,768:
def big_permutation(k): log_permutation(k) | exp;
def big_combination(k): log_combination(k) | exp;
</syntaxhighlight>
'''Examples using the C implementation''':
<pre>
12 | permutation(9) #=> 79833600
</pre>
'''Examples using gojq, the Go implementation'''
<pre>
60 | combinations(30) #=> 118264581564861424
1000 | combination(333) | tostring | "\(.[:40]) ... (\(length) digits in all)"
#=> 5776134553147651669777486323549601722339 ... (275 digits in all)
</pre>
=={{header|Julia}}==
Line 962 ⟶ 1,796:
'''Functions'''
<syntaxhighlight lang="julia">
function Base.binomial{T<:FloatingPoint}(n::T, k::T)
exp(lfact(n) - lfact(n - k) - lfact(k))
Line 973 ⟶ 1,807:
⊞{T<:Real}(n::T, k::T) = binomial(n, k)
⊠{T<:Real}(n::T, k::T) = factorial(n, n-k)
</syntaxhighlight>
'''Main'''
<syntaxhighlight lang="julia">
function picknk{T<:Integer}(lo::T, hi::T)
n = rand(lo:hi)
Line 1,029 ⟶ 1,863:
println(@sprintf " %7.1f ⊞ %7.1f = %10.6e" n k n ⊞ k)
end
</syntaxhighlight>
{{out}}
Line 1,088 ⟶ 1,922:
=={{header|Kotlin}}==
As Kotlin/JVM can use the java.math.BigInteger class, there is no need to use floating point approximations and so we use exact integer arithmetic for all parts of the task.
<
import java.math.BigInteger
Line 1,128 ⟶ 1,962:
System.out.printf("%4d C %-3d = %s%s\n", n, k, s.take(40), e)
}
}</
{{out}}
Line 1,174 ⟶ 2,008:
1000 C 333 = 5776134553147651669777486323549601722339... (235 more digits)
</pre>
=={{header|Lua}}==
{{trans|Perl}}
<syntaxhighlight lang="Lua">
-- Helper function to display results in scientific notation corrected
function eshow(x)
local e = math.floor(x / math.log(10))
local exponentiated = math.exp(x - e * math.log(10))
-- Corrected format specifier from %.8Fe%+d to %.8e, and manually constructing the scientific notation if needed
return string.format("%.8e", exponentiated) .. "e" .. tostring(e)
end
-- The rest of the functions remain the same
-- Define the factorial function for permutations
function P(n, k)
local x = 1
for i = n - k + 1, n do
x = x * i
end
return x
end
-- Define the function for big permutations using logarithms
function P_big(n, k)
local x = 0
for i = n - k + 1, n do
x = x + math.log(i)
end
return eshow(x)
end
-- Define the combinations function
function C(n, k)
local x = 1
for i = 1, k do
x = x * (n - i + 1) / i
end
return x
end
-- Define the function for big combinations using logarithms
function C_big(n, k)
local x = 0
for i = 1, k do
x = x + math.log(n - i + 1) - math.log(i)
end
return math.exp(x)
end
-- Function to showcase the calculations
function showoff(text, code, fname, ...)
local n = {...}
print("\nA sample of " .. text .. " from " .. n[1] .. " to " .. n[#n] .. "")
for _, v in ipairs(n) do
local k = math.floor(v / 3)
print(v, fname .. " " .. k .. " = ", code(v, k))
end
end
-- Examples of usage
showoff("Permutations", P, "P", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
showoff("Combinations", C, "C", 10, 20, 30, 40, 50, 60)
showoff("Permutations", P_big, "P", 5, 50, 500, 1000, 5000, 15000)
showoff("Combinations", C_big, "C", 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
</syntaxhighlight>
{{out}}
<pre>
A sample of Permutations from 1 to 12
1 P 0 = 1
2 P 0 = 1
3 P 1 = 3
4 P 1 = 4
5 P 1 = 5
6 P 2 = 30
7 P 2 = 42
8 P 2 = 56
9 P 3 = 504
10 P 3 = 720
11 P 3 = 990
12 P 4 = 11880
A sample of Combinations from 10 to 60
10 C 3 = 120.0
20 C 6 = 38760.0
30 C 10 = 30045015.0
40 C 13 = 12033222880.0
50 C 16 = 4923689695575.0
60 C 20 = 4.1918445058055e+15
A sample of Permutations from 5 to 15000
5 P 1 = 5.00000000e+00e0
50 P 16 = 1.03017325e+00e26
500 P 166 = 3.53487492e+00e434
1000 P 333 = 5.96932629e+00e971
5000 P 1666 = 6.85674576e+00e6025
15000 P 5000 = 9.64985399e+00e20469
A sample of Combinations from 100 to 1000
100 C 33 = 2.9469242702254e+26
200 C 66 = 7.2697525451696e+53
300 C 100 = 4.158251463258e+81
400 C 133 = 1.2579486841822e+109
500 C 166 = 3.9260283861944e+136
600 C 200 = 2.5060177832203e+164
700 C 233 = 8.1032035633383e+191
800 C 266 = 2.6456233626831e+219
900 C 300 = 1.7433563732974e+247
1000 C 333 = 5.776134553149e+274
</pre>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module PermComb {
Form 80, 50
perm=lambda (x,y) ->{
def i,z
z=1
For i=x-y+1 to x :z*=i:next i
=z
}
fact=lambda (x) ->{
def i,z
z=1
For i=2 to x :z*=i:next i
=z
}
comb=lambda (x as decimal, y as decimal) ->{
If y>x then {
=0
} else.if x=y then {
=1
} else {
if x-y<y then y=x-y
def decimal i, z=1, ym
ym=y
For i=x to x-y+1
z*=i
z=z/ym
ym-- : if ym<1 then ym=1@
next i
=round(z,0)
}
}
Document Doc$
WriteLn("-- Permutations - from 1 to 12")
For i=1 to 12
l$="" : For j=1 to i : l$+= format$("P({0},{1})={2} ",i, j,perm(i, j)) :next j
Writetext(l$)
next i
WriteLn("-- Combinations from 10 to 60")
For i=10 to 60 step 10
l$="" : For j=1 to i step i div 5 : l$+= format$("C({0},{1})={2} ",i, j,comb(i, j)) :next j
Writetext(l$)
Next i
WriteLn("-- Permutations from 5000 to 15000")
For i=5000 to 15000 step 5000
l$="" : For j=10 to 70 step 20: l$+= format$("P({0},{1})={2} ",i, j,perm(i, j)) :next j
Writetext(l$)
Next i
WriteLn("-- Combinations from 200 to 1000")
For i=200 to 1000 step 200
l$="" : For j=20 to 100 step 20: l$+= format$("C({0},{1})={2} ",i, j,comb(i, j)) :next j
Writetext(l$)
Next i
ClipBoard Doc$
Sub WriteText(a$)
doc$=a$+{
}
Report a$
End Sub
Sub WriteLn(a$)
doc$=a$+{
}
Print a$
End Sub
}
PermComb
</syntaxhighlight>
{{out}}
-- Permutations - from 1 to 12
P(1,1)=1
P(2,1)=2 P(2,2)=2
P(3,1)=3 P(3,2)=6 P(3,3)=6
P(4,1)=4 P(4,2)=12 P(4,3)=24 P(4,4)=24
P(5,1)=5 P(5,2)=20 P(5,3)=60 P(5,4)=120 P(5,5)=120
P(6,1)=6 P(6,2)=30 P(6,3)=120 P(6,4)=360 P(6,5)=720 P(6,6)=720
P(7,1)=7 P(7,2)=42 P(7,3)=210 P(7,4)=840 P(7,5)=2520 P(7,6)=5040 P(7,7)=5040
P(8,1)=8 P(8,2)=56 P(8,3)=336 P(8,4)=1680 P(8,5)=6720 P(8,6)=20160 P(8,7)=40320 P(8,8)=40320
P(9,1)=9 P(9,2)=72 P(9,3)=504 P(9,4)=3024 P(9,5)=15120 P(9,6)=60480 P(9,7)=181440 P(9,8)=362880 P(9,9)=362880
P(10,1)=10 P(10,2)=90 P(10,3)=720 P(10,4)=5040 P(10,5)=30240 P(10,6)=151200 P(10,7)=604800 P(10,8)=1814400 P(10,9)=3628800 P(10,10)=3628800
P(11,1)=11 P(11,2)=110 P(11,3)=990 P(11,4)=7920 P(11,5)=55440 P(11,6)=332640 P(11,7)=1663200 P(11,8)=6652800 P(11,9)=19958400 P(11,10)=39916800 P(11,11)=39916800
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 P(12,12)=479001600
-- Combinations from 10 to 60
C(10,1)=10 C(10,3)=120 C(10,5)=252 C(10,7)=120 C(10,9)=10
C(20,1)=20 C(20,5)=15504 C(20,9)=167960 C(20,13)=77520 C(20,17)=1140
C(30,1)=30 C(30,7)=2035800 C(30,13)=119759850 C(30,19)=54627300 C(30,25)=142506
C(40,1)=40 C(40,9)=273438880 C(40,17)=88732378800 C(40,25)=40225345056 C(40,33)=18643560
C(50,1)=50 C(50,11)=37353738800 C(50,21)=67327446062800 C(50,31)=30405943383200 C(50,41)=2505433700
C(60,1)=60 C(60,13)=5166863427600 C(60,25)=51915437974328292 C(60,37)=23385332420868600 C(60,49)=342700125300
-- Permutations from 5000 to 15000
P(5000,10)=9,67807348145655E+36 P(5000,30)=8,53575581200676E+110 P(5000,50)=6,94616656703754E+184 P(5000,70)=5,21383580146195E+258
P(10000,10)=9,95508690556325E+39 P(10000,30)=9,57391540294832E+119 P(10000,50)=8,84526658067387E+199 P(10000,70)=7,850079552152E+279
P(15000,10)=5,74922667554068E+41 P(15000,30)=1,86266591363916E+125 P(15000,50)=5,87565776023335E+208 P(15000,70)=1,80450662858719E+292
-- Combinations from 200 to 1000
C(200,20)=1613587787967350073386147640 C(200,40)=721126811024990370 C(200,60)=286107190317772000463240955 C(200,80)=900482 C(200,100)=478205104
C(400,20)=3558235073 C(400,40)=130321 C(400,60)=74780600187861332802765 C(400,80)=9521248771125 C(400,100)=447355513982663594791392
C(600,20)=2801445153584 C(600,40)=266319106596345 C(600,60)=14409368913 C(600,80)=271441 C(600,100)=52868467287780595308
C(800,20)=1925279023672620 C(800,40)=23121069591511231041 C(800,60)=18702067923763447158 C(800,80)=1193559552292625 C(800,100)=172727802
C(1000,20)=1239329180287869852 C(1000,40)=1937726921514640866484017 C(1000,60)=149470629867337347460963500 C(1000,80)=1269150275532146867313740 C(1000,100)=10088410532027029794548
=={{header|Maple}}==
<syntaxhighlight lang="maple">
comb := proc (n::integer, k::integer)
return factorial(n)/(factorial(k)*factorial(n-k));
Line 1,183 ⟶ 2,237:
return factorial(n)/factorial(n-k);
end proc;
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Combination[n_,k_]:=Binomial[n,k]
Permutation[n_,k_]:=Binomial[n,k]k!
Line 1,193 ⟶ 2,247:
TableForm[Array[Combination,{6,6},{{10,60},{10,60}}],TableHeadings->{Range[10,60,10],Range[10,60,10]}]
{Row[{#,"P",#-2}," "],N@Permutation[#,#-2]}&/@{5,1000,5000,10000,15000}//Grid
{Row[{#,"C",#/2}," "],N@Combination[#,#/2]}&/@Range[100,1000,100]//Grid</
{{out}}
Line 1,237 ⟶ 2,291:
</pre>
Note that Mathematica can easily handle very big numbers with exact integer arithmetic:
<syntaxhighlight lang="mathematica">
Permutation[200000, 100000]
</syntaxhighlight>
{{out}}
The output is 516777 digits longs:
Line 1,247 ⟶ 2,301:
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">П2 <-> П1 -> <-> П7 КПП7 С/П
ИП1 ИП2 - ПП 53 П3 ИП1 ПП 53 ИП3 / В/О
1 ИП1 * L2 21 В/О
ИП1 ИП2 - ПП 53 П3 ИП2 ПП 53 ИП3 * П3 ИП1 ПП 53 ИП3 / В/О
ИП1 ИП2 + 1 - П1 ПП 26 В/О
ВП П0 1 ИП0 * L0 56 В/О</
''Input'': ''x ^ n ^ k В/О С/П'', where ''x'' = 8 for permutations; 20 for permutations with repetitions; 26 for combinations; 44 for combinations with repetitions.
Line 1,259 ⟶ 2,313:
=={{header|Nim}}==
<
proc perm(n, k: int32): BigInt =
Line 1,278 ⟶ 2,332:
echo "P(1000, 969) = ", perm(1000, 969)
echo "C(1000, 969) = ", comb(1000, 969)</
=={{header|PARI/GP}}==
<
permExact(m,n)=factorback([m-n+1..m]);
combExact=binomial;
Line 1,290 ⟶ 2,344:
sample(combExact, 10, 60);
sample(permApprox, 5, 15000);
sample(combApprox, 100, 1000);</
{{out}}
<pre>?sample(permExact, 1, 12);
Line 1,314 ⟶ 2,368:
=={{header|Perl}}==
Although
native integers and floats are limited to what the computer's native types can handle.
As with the
<
use warnings;
Line 1,369 ⟶ 2,423:
sprintf "%.8Fe%+d", exp($x - $e * log(10)), $e;
}
</syntaxhighlight>
{{out}}
<pre style="height:50ex">A sample of Permutations from 1 to 12
1 P 0 = 1
2 P 0 = 1
3 P 1 = 3
4 P 1 = 4
5 P 1 = 5
6 P 2 = 30
7 P 2 = 42
8 P 2 = 56
9 P 3 = 504
10 P 3 = 720
11 P 3 = 990
12 P 4 = 11880
A sample of Combinations from 10 to 60
10 C 3 = 120
20 C 6 = 38760
30 C 10 = 30045015
40 C 13 = 12033222880
50 C 16 = 4923689695575
60 C 20 = 4.1918445058055e+15
A sample of Permutations from 5 to 15000
5 P 1 = 5.00000000e+0
50 P 16 = 1.03017325e+26
500 P 166 = 3.53487492e+434
1000 P 333 = 5.96932629e+971
5000 P 1666 = 6.85674576e+6025
15000 P 5000 = 9.64985399e+20469
A sample of Combinations from 100 to 1000
100 C 33 = 2.94692427022544e+26
200 C 66 = 7.26975254516929e+53
300 C 100 = 4.15825146325788e+81
400 C 133 = 1.25794868418216e+109
500 C 166 = 3.92602838619369e+136
600 C 200 = 2.50601778322159e+164
700 C 233 = 8.10320356333741e+191
800 C 266 = 2.64562336268385e+219
900 C 300 = 1.74335637329697e+247
1000 C 333 = 5.77613455314442e+274</pre>
=={{header|Phix}}==
Translation of Raku/Sidef, same results<br>
Update: there are now builtin routines k_perm(n,k) and choose(n,k), slightly more efficient equivalents of P() and C() respectively.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">P</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">C</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">P</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #004600;">E</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">*</span><span style="color: #004600;">E</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">P_approx</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">C_approx</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">lstirling</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_s</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%.9ge%d"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #004600;">E</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">-</span><span style="color: #000000;">e</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #000080;font-style:italic;">-- Test code</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"=> Exact results:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"P(%d,%d) = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">P</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">10</span> <span style="color: #008080;">to</span> <span style="color: #000000;">60</span> <span style="color: #008080;">by</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C(%d,%d) = %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">C</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"=> Floating point approximations:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">50</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">500</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15000</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"P(%d,%d) = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">P_approx</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">100</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1000</span> <span style="color: #008080;">by</span> <span style="color: #000000;">100</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C(%d,%d) = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_s</span><span style="color: #0000FF;">(</span><span style="color: #000000;">C_approx</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
=> Exact results:
P(1,0) = 1
P(2,0) = 1
P(3,1) = 3
P(4,1) = 4
P(5,1) = 5
P(6,2) = 30
P(7,2) = 42
P(8,2) = 56
P(9,3) = 504
P(10,3) = 720
P(11,3) = 990
P(12,4) = 11880
C(10,3) = 120
C(20,6) = 38760
C(30,10) = 30045014
C(40,13) = 12033222880
C(50,16) = 4923689695575
C(60,20) = 4191844505805496
=> Floating point approximations:
P(5,1) = 5e0
P(50,16) = 1.03017326e26
P(500,166) = 3.53487492e434
P(1000,333) = 5.96932629e971
P(5000,1666) = 6.85674576e6025
P(15000,5000) = 9.64985399e20469
C(100,33) = 2.94692433e26
C(200,66) = 7.26975256e53
C(300,100) = 4.15825147e81
C(400,133) = 1.25794868e109
C(500,166) = 3.92602839e136
C(600,200) = 2.50601778e164
C(700,233) = 8.10320356e191
C(800,266) = 2.64562336e219
C(900,300) = 1.74335637e247
C(1000,333) = 5.77613455e274
</pre>
=={{header|Python}}==
==={{libheader|SciPy}}===
<syntaxhighlight lang="python">from __future__ import print_function
from scipy.misc import factorial as fact
from scipy.misc import comb
def perm(N, k, exact=0):
return comb(N, k, exact) * fact(k, exact)
exact=True
print('Sample Perms 1..12')
for N in range(1, 13):
k = max(N-2, 1)
print('%iP%i =' % (N, k), perm(N, k, exact), end=', ' if N % 5 else '\n')
print('\n\nSample Combs 10..60')
for N in range(10, 61, 10):
k = N-2
print('%iC%i =' % (N, k), comb(N, k, exact), end=', ' if N % 50 else '\n')
exact=False
print('\n\nSample Perms 5..1500 Using FP approximations')
for N in [5, 15, 150, 1500, 15000]:
k = N-2
print('%iP%i =' % (N, k), perm(N, k, exact))
print('\nSample Combs 100..1000 Using FP approximations')
for N in range(100, 1001, 100):
k = N-2
print('%iC%i =' % (N, k), comb(N, k, exact))
</syntaxhighlight>
{{out}}
<pre>Sample Perms 1..12
1P1 = 1, 2P1 = 2, 3P1 = 3, 4P2 = 12, 5P3 = 60
6P4 = 360, 7P5 = 2520, 8P6 = 20160, 9P7 = 181440, 10P8 = 1814400
11P9 = 19958400, 12P10 = 239500800,
Sample Combs 10..60
10C8 = 45, 20C18 = 190, 30C28 = 435, 40C38 = 780, 50C48 = 1225
60C58 = 1770,
Sample Perms 5..1500 Using FP approximations
5P3 = 60.0
15P13 = 653837184000.0
150P148 = 2.85669197822e+262
1500P1498 = inf
15000P14998 = inf
Sample Combs 100..1000 Using FP approximations
100C98 = 4950.0
200C198 = 19900.0
300C298 = 44850.0
400C398 = 79800.0
500C498 = 124750.0
600C598 = 179700.0
700C698 = 244650.0
800C798 = 319600.0
900C898 = 404550.0
1000C998 = 499500.0</pre>
=={{header|R}}==
R has most of this built in. The documentation for choose warns that it only calculates its result directly if k is small. Skimming [https://github.com/wch/r-source/blob/trunk/src/nmath/choose.c what appears to be the source code] suggests that 29 is the highest "small" k. This means that we can solve both tasks with little more than R's choose.
<syntaxhighlight lang="rsplus">perm <- function(n, k) choose(n, k) * factorial(k)
print(perm(seq(from = 3, to = 12, by = 3), seq(from = 2, to = 8, by = 2)))
print(choose(seq(from = 10, to = 60, by = 10), seq(from = 3, to = 18, by = 3)))
print(perm(seq(from = 1500, to = 15000, by = 1500), seq(from = 55, to = 100, by = 5)))
print(choose(seq(from = 100, to = 1000, by = 150), seq(from = 70, to = 100, by = 5)))</syntaxhighlight>
{{out}}
<pre>> print(perm(seq(from = 3, to = 12, by = 3), seq(from = 2, to = 8, by = 2)))
[1] 6 360 60480 19958400
> print(choose(seq(from = 10, to = 60, by = 10), seq(from = 3, to = 18, by = 3)))
[1] 1.200000e+02 3.876000e+04 1.430715e+07 5.586853e+09 2.250830e+12 9.250296e+14
> print(perm(seq(from = 1500, to = 15000, by = 1500), seq(from = 55, to = 100, by = 5)))
[1] 1.777313e+174 2.340618e+208 1.807619e+237 1.972900e+264 2.940161e+290 Inf Inf Inf Inf Inf
> print(choose(seq(from = 100, to = 1000, by = 150), seq(from = 70, to = 100, by = 5)))
[1] 2.937234e+25 1.158870e+65 4.228142e+85 3.202323e+101 1.944760e+115 8.124447e+127 6.385051e+139</pre>
We can notice some interesting things about R from this task. Despite being a mathematical programming language:
#R does not have any function like perm built in.
#R does not have big integers by default and will return Inf without warning.
#R's documentation does not tell us exactly when choose's behaviour changes.
=={{header|Racket}}==
Racket's "math" library has two functions that compute nCk and nPk.
They work only on integers, but since Racket supports unlimited integers
there is no need for a floating point estimate:
<syntaxhighlight lang="racket">
#lang racket
(require math)
(define C binomial)
(define P permutations)
(C 1000 10) ; -> 263409560461970212832400
(P 1000 10) ; -> 955860613004397508326213120000
</syntaxhighlight>
(I'll spare this page from yet another big listing of samples...)
=={{header|
(formerly Perl 6)
Raku can't compute arbitrary large floating point values, thus we will use logarithms, as is often needed when dealing with combinations. We'll also use a Stirling method to approximate <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
<syntaxhighlight lang="raku"
multi C($n, $k) { P($n, $k) / [*] 1 .. $k }
Line 1,425 ⟶ 2,719:
my $p = $n div 3;
say "C($n, $p) = ", C($n, $p, :float);
}</
{{out}}
<pre>Exact results:
Line 1,464 ⟶ 2,758:
C(900, 300) = 1.74335637E+247
C(1000, 333) = 5.77613455E+274</pre>
=={{header|REXX}}==
The hard part of this REXX program was coding the '''DO''' loops for the various ranges.
<
numeric digits 100 /*use 100 decimal digits of precision. */
Line 1,644 ⟶ 2,804:
if x-y <y then y=x - y /*switch things around for speed. */
call .combPerm /*call subroutine to do heavy lifting. */
return _ / !(y) /*just perform one last division. */</
'''output'''
<pre>
Line 1,693 ⟶ 2,853:
C(900,1)=900 C(900,181)=5.1402207737939392620E+194 C(900,361)=4.6256239559539226532E+261 C(900,541)=2.0577328996911473555E+261 C(900,721)=3.2260054093505652100E+193
C(1000,1)=1000 C(1000,201)=2.6336937554862900107E+216 C(1000,401)=7.4293352412781479131E+290 C(1000,601)=3.3046738011675400053E+290 C(1000,801)=1.6522236106515115238E+215
</pre>
=={{header|RPL}}==
≪ / LAST ROT * - ≫ '<span style="color:blue">BMOD</span>' STO
≪ → n k
≪ # 1d
n k - 1 + n '''FOR''' j
j * '''NEXT'''
≫ ≫ '<span style="color:blue">BPERM</span>' STO
≪ → n k
≪ # 1d
DUP n k - 1 + n '''FOR''' j
j *
'''WHILE''' DUP2 SWAP <span style="color:blue">BMOD</span> # 0d == 3 PICK k R→B ≤ AND '''REPEAT'''
OVER / SWAP 1 + SWAP '''END'''
'''NEXT''' SWAP DROP
≫ ≫ '<span style="color:blue">BCOMB</span>' STO
To mitigate risk of data overflow with 64-bits integers, the inner <code>WHILE..REPEAT</code> loop permanently attempts to divide the current numerator by increasing values of k.
≪ {} 1 11 '''FOR''' j 12 j <span style="color:blue">BPERM</span> + '''NEXT''' ≫
≪ {} 10 50 '''FOR''' j 60 j <span style="color:blue">BCOMB</span> + 10 '''STEP''' ≫
{{out}}
<pre>
2: { # 12d # 132d # 1320d # 11880d # 95040d # 665280d # 3991680d # 19958400d # 79833600d # 239500800d # 479001600d }
1: { # 75394027566d # 4191844505805495d # 118264581564861424d # 4191844505805495d # 75394027566d }
</pre>
Floating point approximations can be obtained by the built-in commands, provided that the result does not go beyond 1E500:
1000 150 PERM
1000 400 COMB
{{out}}
<pre>
2: 7.67405677335E444
1: 4.96527238625E290
</pre>
=={{header|Ruby}}==
Float calculation as Tcl.
<
class Integer
Line 1,735 ⟶ 2,930:
#Fixnum has no maximum:
p 15000.permutation(74) #=> 896237613852967826239917238565433149353074416025197784301593335243699358040738127950872384197159884905490054194835376498534786047382445592358843238688903318467070575184552953997615178973027752714539513893159815472948987921587671399790410958903188816684444202526779550201576117111844818124800000000000000000000
</syntaxhighlight>
Ruby's Arrays have a permutation and a combination method which result in (lazy) enumerators. These Enumerators have a "size" method, which returns the size of the enumerator, or nil if it can’t be calculated lazily. (Since Ruby 2.0)
<
=={{header|Rust}}==
{{trans|zig}}
Almost a verbatim port... the logic is retained but I renounce anyway to use `io::stdout()`, `println!()` is enough. I wonder why f64?!
<syntaxhighlight lang="rust">
fn perm(n: f64, k: f64) -> f64 {
let mut result: f64 = 1.0;
let mut i: f64 = 0.0;
while i < k {
result *= n - i;
i += 1.0;
}
result
}
fn comb(n: f64, k: f64) -> f64 {
perm(n, k) / perm(k, k)
}
fn main() {
const P: f64 = 12.0;
const C: f64 = 60.0;
let mut j: f64 = 1.0;
let mut k: f64 = 10.0;
while j < P {
println!("P({},{}) = {}", P, j, perm(P, j).floor());
j += 1.0;
}
while k < C {
println!("C({},{}) = {}", C, k, comb(C, k).floor());
k += 10.0;
}
}
</syntaxhighlight>
{{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) = 118264581564861420
C(60,40) = 4191844505805495
C(60,50) = 75394027566
</pre>
=={{header|Scheme}}==
<
(define (combinations n k)
(do ((i 0 (+ 1 i))
Line 1,766 ⟶ 3,025:
(display "C(1000,1000) = ") (display (combinations 1000 1000)) (newline)
(display "C(15000,14998) = ") (display (combinations 15000 14998)) (newline)
</syntaxhighlight>
{{out}}
Line 1,786 ⟶ 3,045:
=={{header|Sidef}}==
{{trans|
<
func C(n, k) { binomial(n, k) }
Line 1,831 ⟶ 3,090:
var p = n//3
say "C(#{n}, #{p}) = #{C_approx(n, p)}"
}</
{{out}}
<pre>
Line 1,876 ⟶ 3,135:
The '''[https://www.stata.com/help.cgi?mf_comb comb]''' function is builtin. Here is an implementation, together with perm:
<
return(exp(lnfactorial(n)-lnfactorial(k)-lnfactorial(n-k)))
}
Line 1,882 ⟶ 3,141:
real scalar perm(n, k) {
return(exp(lnfactorial(n)-lnfactorial(n-k)))
}</
=={{header|Swift}}==
{{trans|Kotlin}}
Using AttaSwift's BigInt
<syntaxhighlight lang="swift">import BigInt
func permutations(n: Int, k: Int) -> BigInt {
let l = n - k + 1
guard l <= n else {
return 1
}
return (l...n).reduce(BigInt(1), { $0 * BigInt($1) })
}
func combinations(n: Int, k: Int) -> BigInt {
let fact = {() -> BigInt in
guard k > 1 else {
return 1
}
return (2...k).map({ BigInt($0) }).reduce(1, *)
}()
return permutations(n: n, k: k) / fact
}
print("Sample of permutations from 1 to 12")
for i in 1...12 {
print("\(i) P \(i / 3) = \(permutations(n: i, k: i / 3))")
}
print("\nSample of combinations from 10 to 60")
for i in stride(from: 10, through: 60, by: 10) {
print("\(i) C \(i / 3) = \(combinations(n: i, k: i / 3))")
}
print("\nSample of permutations from 5 to 15,000")
for i in [5, 50, 500, 1000, 5000, 15000] {
let k = i / 3
let res = permutations(n: i, k: k).description
let extra = res.count > 40 ? "... (\(res.count - 40) more digits)" : ""
print("\(i) P \(k) = \(res.prefix(40))\(extra)")
}
print("\nSample of combinations from 100 to 1000")
for i in stride(from: 100, through: 1000, by: 100) {
let k = i / 3
let res = combinations(n: i, k: k).description
let extra = res.count > 40 ? "... (\(res.count - 40) more digits)" : ""
print("\(i) C \(k) = \(res.prefix(40))\(extra)")
}</syntaxhighlight>
{{out}}
<pre>Sample of permutations from 1 to 12
1 P 0 = 1
2 P 0 = 1
3 P 1 = 3
4 P 1 = 4
5 P 1 = 5
6 P 2 = 30
7 P 2 = 42
8 P 2 = 56
9 P 3 = 504
10 P 3 = 720
11 P 3 = 990
12 P 4 = 11880
Sample of combinations from 10 to 60
10 C 3 = 120
20 C 6 = 38760
30 C 10 = 30045015
40 C 13 = 12033222880
50 C 16 = 4923689695575
60 C 20 = 4191844505805495
Sample of permutations from 5 to 15,000
5 P 1 = 5
50 P 16 = 103017324974226408345600000
500 P 166 = 3534874921742942787609361826601762306844... (395 more digits)
1000 P 333 = 5969326288503415089039701765900784280998... (932 more digits)
5000 P 1666 = 6856745757255674275484536940248896062234... (5986 more digits)
15000 P 5000 = 9649853988727493922014858805931295980792... (20430 more digits)
Sample of combinations from 100 to 1000
100 C 33 = 294692427022540894366527900
200 C 66 = 7269752545169278341527066651192738976755... (14 more digits)
300 C 100 = 4158251463258564744783383526326405580280... (42 more digits)
400 C 133 = 1257948684182108702133348475651965004491... (70 more digits)
500 C 166 = 3926028386194422755220408345072331428197... (97 more digits)
600 C 200 = 2506017783221402805005616770513228835202... (125 more digits)
700 C 233 = 8103203563339599904740453644031138232944... (152 more digits)
800 C 266 = 2645623362683627034288829299556124255091... (180 more digits)
900 C 300 = 1743356373296446642960730765085718347630... (208 more digits)
1000 C 333 = 5776134553147651669777486323549601722339... (235 more digits)</pre>
=={{header|Tcl}}==
Tcl doesn't allow the definition of new infix operators, so we define <math>P</math> and <math>C</math> as ordinary functions. There are no problems with loss of significance though: Tcl has supported arbitrary precision integer arithmetic since 8.5.
{{tcllib|math}}
<
proc tcl::mathfunc::P {n k} {
set t 1
Line 1,911 ⟶ 3,276:
proc tcl::mathfunc::fC {n k} {
expr {exp(lnGamma($n+1) - lnGamma($n-$k+1) - lnGamma($k+1))}
}</
Demonstrating:
<
puts "A sample of Permutations from 1 to 12:"
for {set i 4} {$i <= 12} {incr i} {
Line 1,938 ⟶ 3,303:
set iii [expr {$i - int(sqrt($i))}]
puts "$i C $ii = [expr {fC($i,$ii)}], $i C $iii = [expr {fC($i,$iii)}]"
}</
{{out}}
<pre>
Line 1,989 ⟶ 3,354:
=={{header|VBScript}}==
<
dim i,j
Wscript.StdOut.WriteLine "-- Long Integer - Permutations - from 1 to 12"
Line 2,047 ⟶ 3,412:
comb=perm(x,y)/fact(y)
end if
end function 'comb</
{{out}}
<pre style="height:40ex">
Line 2,084 ⟶ 3,449:
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|2013}}
<
Imports System.Numerics 'BigInteger
Module CombPermRc
Line 2,152 ⟶ 3,517:
End Function 'CombBig
End Module</
{{out}}
<pre style="height:40ex">
Line 2,184 ⟶ 3,549:
--
C(5000,4000)=57462357505803375604893834658665168251899919793850512934468881710397678593302188064618445132583370701755893065787216750992391223467601994741594656878559929037277303674963658032197224327768110236651567704673226756781828332650887849150208195780031161286578505113618731045004523840401144118298192191997565735245181433457469532981432785237769191864102953974244072964471109551273603780184330987071947790993108191904370472373403157802158903129815170101708451875442019845175637901995588390614304812103202403626211504997668649346891167495657556154392183988627948442807346603688457854135114491955258804187129028256547543888109987151649038111791932035229202856007767332717845596528598314477979861265222941138323298702349967224867703420888363395662988291273283611081068577160905840445308086112429900453394212790633910614322699210850302387512579976209123523546689147207974269396548873838155355768985614160932799226261509866933143702889005270480654844312094564956178277998090168826124850606847021667494019587077659107276117413835912767949017954979839258481340540145909025953956582025656306426226560
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-iterate}}
<syntaxhighlight lang="wren">import "./big" for BigInt
import "./fmt" for Fmt
import "./iterate" for Stepped
var perm = Fn.new { |n, k|
if (n <= 0 || k < 0) Fiber.abort("Invalid argument(s).")
if (k == 0) return BigInt.one
return (n-k+1..n).reduce(BigInt.one) { |acc, i| acc * BigInt.new(i) }
}
var comb = Fn.new { |n, k|
if (n <= 0 || k < 0) Fiber.abort("Invalid argument(s).")
var fact = BigInt.one
if (k > 1) fact = (2..k).reduce(BigInt.one) { |acc, i| acc * BigInt.new(i) }
return perm.call(n, k) / fact
}
System.print("A sample of permutations from 1 to 12:")
for (n in 1..12) Fmt.print("$2d P $-2d = $i", n, (n/3).floor, perm.call(n, (n/3).floor))
System.print("\nA sample of combinations from 10 to 60:")
for (n in Stepped.new(10..60, 10)) {
Fmt.print("$2d C $-2d = $i", n, (n/3).floor, comb.call(n, (n/3).floor))
}
System.print("\nA sample of permutations from 5 to 15000:")
var na = [5, 50, 500, 1000, 5000, 15000]
for (n in na) {
var k = (n/3).floor
var s = perm.call(n, k).toString
var l = s.count
var e = (l <= 40) ? "" : "... (%(l - 40) more digits)"
Fmt.print("$5d P $-4d = $s$s", n, k, s.take(40).join(), e)
}
System.print("\nA sample of combinations from 100 to 1000:")
for (n in Stepped.new(100..1000, 100)) {
var k = (n/3).floor
var s = comb.call(n, k).toString
var l = s.count
var e = (l <= 40) ? "" : "... (%(l - 40) more digits)"
Fmt.print("$4d C $-3d = $s$s", n, k, s.take(40).join(), e)
}</syntaxhighlight>
{{out}}
<pre>
A sample of permutations from 1 to 12:
1 P 0 = 1
2 P 0 = 1
3 P 1 = 3
4 P 1 = 4
5 P 1 = 5
6 P 2 = 30
7 P 2 = 42
8 P 2 = 56
9 P 3 = 504
10 P 3 = 720
11 P 3 = 990
12 P 4 = 11880
A sample of combinations from 10 to 60:
10 C 3 = 120
20 C 6 = 38760
30 C 10 = 30045015
40 C 13 = 12033222880
50 C 16 = 4923689695575
60 C 20 = 4191844505805495
A sample of permutations from 5 to 15000:
5 P 1 = 5
50 P 16 = 103017324974226408345600000
500 P 166 = 3534874921742942787609361826601762306844... (395 more digits)
1000 P 333 = 5969326288503415089039701765900784280998... (932 more digits)
5000 P 1666 = 6856745757255674275484536940248896062234... (5986 more digits)
15000 P 5000 = 9649853988727493922014858805931295980792... (20430 more digits)
A sample of combinations from 100 to 1000:
100 C 33 = 294692427022540894366527900
200 C 66 = 7269752545169278341527066651192738976755... (14 more digits)
300 C 100 = 4158251463258564744783383526326405580280... (42 more digits)
400 C 133 = 1257948684182108702133348475651965004491... (70 more digits)
500 C 166 = 3926028386194422755220408345072331428197... (97 more digits)
600 C 200 = 2506017783221402805005616770513228835202... (125 more digits)
700 C 233 = 8103203563339599904740453644031138232944... (152 more digits)
800 C 266 = 2645623362683627034288829299556124255091... (180 more digits)
900 C 300 = 1743356373296446642960730765085718347630... (208 more digits)
1000 C 333 = 5776134553147651669777486323549601722339... (235 more digits)
</pre>
=={{header|zig}}==
<syntaxhighlight lang="zig> const std = @import("std");
const num = f64;
pub fn perm(n: num, k: num) num {
var result: num = 1;
var i: num = 0;
while (i < k) : (i += 1) {
result *= n - i;
}
return result;
}
pub fn comb(n: num, k: num) num {
return perm(n, k) / perm(k, k);
}
pub fn main() !void {
var stdout = std.io.getStdOut().writer();
const p: num = 12;
const c: num = 60;
var j: num = 1;
var k: num = 10;
while (j < p) : (j += 1) {
try stdout.print("P({d},{d}) = {d}\n", .{ p, j, @floor(perm(p, j)) });
}
while (k < c) : (k += 10) {
try stdout.print("C({d},{d}) = {d}\n", .{ c, k, @floor(comb(c, k)) });
}
}</syntaxhighlight>
{{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) = 118264581564861420
C(60,40) = 4191844505805495
C(60,50) = 75394027566
</pre>
|