Combinations and permutations: Difference between revisions

Add Lua implementation
m (→‎{{header|Wren}}: Wren-trait -> Wren-iterate)
(Add Lua implementation)
 
(20 intermediate revisions by 10 users not shown)
Line 259:
(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}}==
BracmatThis cannotsolution shows results from bignum as well handleas floating point numbersimplementations. Instead,If thisbignum solutionanswers showsare very big, only the first 50 digits and a count of the digits that are not shown are shown. The bignum answers are shown as Cn and Pn, while the floating point answers are shown as Cf and Pf. Answers are aligned to make it easy to compare the decimals.
<syntaxhighlight lang="bracmat">( ( C
= n k coef
. !arg:(?n,?k)
Line 275 ⟶ 336:
& !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 287 ⟶ 377:
& !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 297 ⟶ 406:
' ( 10+!i:~>60:?i
& div$(!i.3):?k
& out$(!i CCn !k "= " C$(!i,!k))
& out$(!i Cf !k "=" (binom..go)$(!i,!k))
)
& ( displayBig
Line 309 ⟶ 419:
' ( !is:%?i ?is
& div$(!i.3):?k
& out
& out$(str$(!i " P " !k " = " displayBig$(P$(!i,!k))))
$ ( str
$ (!i " Pn " !k " = " displayBig$(P$(!i,!k)))
)
& out
$ ( str
$ (!i " Pf " !k " = " (permu..go)$(!i,!k))
)
)
& 0:?i
Line 315 ⟶ 432:
' ( 100+!i:~>1000:?i
& div$(!i.3):?k
& out
& out$(str$(!i " C " !k " = " displayBig$(C$(!i,!k))))
$ ( 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 331 ⟶ 455:
11 P 3 = 990
12 P 4 = 11880
10 CCn 3 = 120
10 Cf 3 = 1.1999999999999999E+02
20 C 6 = 38760
3020 CCn 106 = 30045015 38760
20 Cf 6 = 3.8759999999999993E+04
40 C 13 = 12033222880
5030 CCn 1610 = 4923689695575 30045015
30 Cf 10 = 3.0045014999999989E+07
60 C 20 = 4191844505805495
540 PCn 113 = 5 12033222880
40 Cf 13 = 1.2033222880000000E+10
50 P 16 = 103017324974226408345600000
50 Cn 16 = 4923689695575
500 P 166 = 35348749217429427876093618266017623068440028791060... (385 more digits)
50 Cf 16 = 4.9236896955750000E+12
1000 P 333 = 59693262885034150890397017659007842809981894765670... (922 more digits)
60 Cn 20 = 4191844505805495
5000 P 1666 = 68567457572556742754845369402488960622341567102448... (5976 more digits)
60 Cf 20 = 4.1918445058054930E+15
15000 P 5000 = 96498539887274939220148588059312959807922816886808... (20420 more digits)
5 Pn 1 = 5
100 C 33 = 294692427022540894366527900
5 Pf 1 = 5.0000000000000000E+00
200 C 66 = 72697525451692783415270666511927389767550269141935... (4 more digits)
50 Pn 16 = 103017324974226408345600000
300 C 100 = 41582514632585647447833835263264055802804660057436... (32 more digits)
50 Pf 16 = 1.0301732497422640E+26
400 C 133 = 12579486841821087021333484756519650044917494358375... (60 more digits)
500 CPn 166 = 39260283861944227552204083450723314281973490135301 35348749217429427876093618266017623068440028791060... (87385 more digits)
500 Pf 166 = INF
600 C 200 = 25060177832214028050056167705132288352025510250879... (115 more digits)
7001000 CPn 233333 = 81032035633395999047404536440311382329449203119421 59693262885034150890397017659007842809981894765670... (142922 more digits)
1000 Pf 333 = INF
800 C 266 = 26456233626836270342888292995561242550915240450150... (170 more digits)
9005000 CPn 3001666 = 17433563732964466429607307650857183476303419689548 68567457572556742754845369402488960622341567102448... (1985976 more digits)
5000 Pf 1666 = INF
1000 C 333 = 57761345531476516697774863235496017223394580195002... (225 more digits)</pre>
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}}==
Line 383 ⟶ 530:
return 0;
}</syntaxhighlight>
 
 
=={{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++}}==
Line 552 ⟶ 860:
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}}==
Line 1,278 ⟶ 1,656:
 
=={{header|jq}}==
Currently,The C implementation of jq approximates large integers by IEEE 754 64-bit floats, and only supports tgamma (true gamma). Thus beyond about 1e308 all accuracy is lost.
and only supports tgamma (true gamma), and thus beyond about 1e308 all accuracy is lost.
<syntaxhighlight lang="jq">def permutation(k): . as $n
 
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 1,298 ⟶ 1,683:
def big_permutation(k): log_permutation(k) | exp;
 
def big_combination(k): log_combination(k) | exp;</syntaxhighlight>
</syntaxhighlight>
'''Examples''':
'''Examples using the C implementation''':
12 | permutation(9) #=> 79833600
<pre>
12 | permutation(9) #=> 79833600
 
12 | big_permutation(9) #=> 79833599.99999964
 
60 | combination(53) #=> 386206920
 
60 | big_combination(53) #=> 386206920.0000046
 
145 | big_permutation(133) #=> 1.6801459655817e6801459655817956e+243
 
170 | big_permutation(133) #=> 5.272846415658284e+263
</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 1,527 ⟶ 1,922:
900 C 300 = 1743356373296446642960730765085718347630... (208 more digits)
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>
 
Line 2,261 ⟶ 2,768:
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>
 
Line 2,306 ⟶ 2,848:
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)
<syntaxhighlight lang="ruby">(1..60).to_a.combination(53).size #=> 386206920</syntaxhighlight>
 
=={{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}}==
Line 2,865 ⟶ 3,471:
{{libheader|Wren-fmt}}
{{libheader|Wren-iterate}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
import "./iterate" for Stepped
 
var perm = Fn.new { |n, k|
Line 2,952 ⟶ 3,558:
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>
337

edits