On Nov 15, 12:35 am, "Ph. B."
Post by Ph. B.Post by jodlerenHi all
Say, I have some numbers, 1,2,4,5,6 and I need all possible 4-number
1,2,4,5
1,2,4,6
1,2,6,5
1,6,4,5
6,2,4,5
What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?
Sonnich
Hi Sonnich,
This is a combinatorial problem...
I think it depends on the correct assumption. What exactly do you need ?
It could be "Counts", "Arrangements", "Combinations"...
IMHO, in your case, i think it is a "Combinations without repetition"
C(n,k) = n! / (k! * (n - k)!)
C(5,4) = 5! / (4! * (5 - 4)!) = 120 / (24 * 1) = 5 combinations
1,2,4,5
1,2,4,6
1,2,5,6
1,4,5,6
2,4,5,6
This is for theory.
(a form with a TMemo and a TButton)
procedure TForm1.Button1Click(Sender: TObject);
const
Values : array[1..5] of integer = (1, 2, 4, 5, 6);
var
i, j, k, l : integer;
begin
Memo1.Clear;
Memo1.Lines.Add('Combinations C(5,4) for values : 1, 2, 4, 5, 6');
Memo1.Lines.Add('');
for i := 1 to 2 do // 2 = n - k + (k - 3)
for j := i + 1 to 3 do // 3 = n - k + (k - 2)
for k := j + 1 to 4 do // 4 = n - k + (k - 1)
for l := k + 1 to 5 do // 5 = n - k + k
Memo1.Lines.Add(
IntToStr(Values[i]) + ', ' +
IntToStr(Values[j]) + ', ' +
IntToStr(Values[k]) + ', ' +
IntToStr(Values[l]));
end;
Finally, you have to adapt this small example to yours needs...
Regards,
Philippe
Thanks
I came up with this, and it seems to work:
Given:
RowCount - numbers in row - here 4
FTotal - numbers available - here 5
FNumbers - then numbers in use.
var
Counters: array of integer;
i, j, iCounter, k: integer;
sTemp: string;
bGo: boolean;
begin
try
SetLength(Counters, RowCount);
for i := 0 to RowCount-1 do
Counters[i] := i;
k := 0;
while Counters[0] < (FTotal - RowCount + 1) do
begin
sTemp := '';
for i := 0 to RowCount-1 do
sTemp := sTemp + IntToStr(FNumbers[Counters[i]]) + ' ';
Memo1.Lines.Add(sTemp);
iCounter := RowCount - 1;
repeat
if Counters[iCounter] < (FTotal-(RowCount-iCounter)) then
begin
Inc(Counters[iCounter]);
for j := iCounter +1 to RowCount-1 do
Counters[j] := Counters[j - 1] + 1;
bGo := False;
end
else
begin
Dec(iCounter);
bGo := (iCounter > -1);
if iCounter = -1 then // stop it all
Counters[0] := FTotal + 1;
end;
until not bGo;
Inc(k); // count items generated
end;
Edit1.Text := IntToStr(k);
except
end;
end;