Combinations and permutations: Difference between revisions

(Add Lua implementation)
 
Line 64:
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>
 
19

edits