Permutations with some identical elements: Difference between revisions

m
→‎{{header|Pascal}}: correct search index to count of different elements gblMaxUsedIdx not gblMaxCardsUsed elements.Saves some time
(added Pascal)
m (→‎{{header|Pascal}}: correct search index to count of different elements gblMaxUsedIdx not gblMaxCardsUsed elements.Saves some time)
Line 540:
Most time consuming is appending to the stringlist.So I limited that to 10000<BR>
One can use the string directly in EvaluatePerm.
<lang pascal>program Perm_k_outof_nPermWithRep;//of different length
{$IFDEF FPC}
{$mode Delphi} {$Optimization ON,All}
{$Optimization ON,All}
{$ELSE}
{$APPTYPE CONSOLE}
Line 562 ⟶ 563:
Elem : tDiffCardCount;
end;
tRemSet = array [low(tDeckIndex)..High(tDeckIndex)] of tSetElem;
 
tRemSet = array [low(tDeckIndex)..High(tDeckIndex)] of tSetElem;
tpRemSet = ^tRemSet;
tRemainSet = array [low(tSequenceIndex)..High(tSequenceIndex)] of tRemSet;
tCardSequence = array [low(tSequenceIndex)..High(tSequenceIndex)] of tDiffCardCount;
 
var
{$ALIGN 32}
RemainSets : tRemainSet;
CardString : AnsiString;
CS : pchar;
sl :TStringList;
gblMaxCardsUsed,
gblMaxUsedIdx,
gblPermCount : NativeInt;
Line 602 ⟶ 606:
procedure SetInit(var ioSet:tRemSet;const inSet:tRemSet);
var
i,j,k,sum : integer;
begin
ioSet := inSet;
sum := 0;
k := 0;
write('Initial set : ');
For i := Low(ioSet) to High(ioSet) do
Begin
j := inSet[i].ElemCount;
if j <> 0 then
inc(pSetElemk);
sum += j;
For j := j downto 1 do
write(chr(inSet[i].Elem));
end;
gblMaxUsedIdxgblMaxCardsUsed := sum;
gblMaxUsedIdx := k;
writeln(' lenght: ',gblMaxUsedIdxsum,' different elements : ',k);
end;
 
procedure EvaluatePerm;
Begin
//append at maxmaximal 10000 strings
if gblPermCount < 10000 then
sl.append(CS);
Line 627 ⟶ 635:
procedure Permute(depth,MaxCardsUsed:NativeInt);
var
pSetElem : tpRemSet;//^tSetElem;
i : NativeInt;
begin
Line 633 ⟶ 641:
pSetElem := @RemainSets[depth];
repeat
if pSetElem^[i].Elemcount <> 0 then begin
//take one of the same elements of the stack
//insert in result here string
CS[depth] := chr(pSetElem^[i].Elem);
//done one permutation
IF depth = MaxCardsUsed then
Line 645 ⟶ 653:
else
begin
RemainSets[depth+1]:= RemainSets[depth];
dec(pSetElem^.ElemCount);
//re-insertremove thatone element
RemainSets[depth+1]:= RemainSets[depth];
dec(pSetElem^RemainSets[depth+1][i].ElemCount);
Permute(depth+1,MaxCardsUsed);
//re-insert that element
inc(pSetElem^.ElemCount);
end;
end;
//move on to the next digitElem
inc(pSetElem);
inc(i);
until i >= gblMaxUsedIdx;
Line 661 ⟶ 667:
Begin
gblpermCount := 0;
if MaxCardsUsed > gblMaxUsedIdxgblMaxCardsUsed then
MaxCardsUsed := gblMaxUsedIdxgblMaxCardsUsed;
 
if MaxCardsUsed>0 then
Line 694 ⟶ 700:
 
SetInit(RemainSets[0], Manifolds);
j := gblMaxUsedIdxgblMaxCardsUsed;
writeln('Count of elements: ',j);
while j > 1 do
Line 711 ⟶ 717:
 
SetInit(RemainSets[0], Manifolds);
j := gblMaxUsedIdxgblMaxCardsUsed;
writeln('Count of elements: ',j);
while j > 1 do
Line 722 ⟶ 728:
dec(j);
end;
 
//extend by 3 more elements
with Manifolds[3] do
Line 735 ⟶ 742:
Elemcount := 1; Elem := Ord('6');
end;
 
SetInit(RemainSets[0], Manifolds);
j := gblMaxUsedIdxgblMaxCardsUsed;
writeln('Count of elements: ',j);
sl.clear;
Line 812 ⟶ 818:
Length 12 Permutations 3326400
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
Anonymous user