# combination of numbers

Hi

I want to write some code that give me all the combinations of some numbers.

For exemple.

I have 30 numbers (1 to 30) and from those numbers i need all the
combinations
in 6 colums.

1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
1 2 3 4 5 9
and so on until

25 26 27 28 29 30

What is the best way to do that.  do i have to use 4 loops.

Regards Mario

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
The basic stuff to solve your problem is in the thread "I need a crazy algorithm."
0
Commented:
Hi,
where do you want to place those, it makes

30!/(24!) = 427518000 combinations...

anyway this one just computes them but does nothing on them.in each pass the sequence is constructed in seq variable from index 0 to 5. NEWER TRY TO STORE IT....I gave the size above and you may crash your system...

you may try to make it nonrecursive to make it faster, it requires some work though, it requires about 4 minitues to compute whole combinations on my PII 300 from delphi ide...

const
LBase = 1;
UBase = 30;
DigitCnt = 6;
type
TNums = LBase..UBase;
TNumSet = set of TNums;
var
Seq: array[0..DigitCnt - 1] of Integer;
IterCnt : Integer;

// CAUTION : this proc is recursive and may divide
// up to UBase-LBase+1 level so watch your stack!!!
// change the constants above to find any general
// combinations...

procedure DigRecurse(dset: TNumSet; digit: Integer);
var
I: Integer;
begin
if Application.Terminated then Exit;
if dset <> [] then
begin
if digit < DigitCnt then
begin
for I := LBase to UBase do
begin
if i in dset then
begin
dset := dset - [i];
seq[digit] := i;
DigRecurse(dset, digit + 1);
dset := dset + [i];
end;
end;
end else
begin
// we have reach the end of a single sequence so
// !!! do what ever with your seq here !!!
// note : it is wise to process messages here
// otherwise application loose its responsiveness
// your current number is in seq[0] to seq[DigitCnt-1]
Inc(IterCnt);
if IterCnt mod 10000 = 0 then
Application.ProcessMessages;
end;
end;
end;

// and use it as follows

procedure TForm1.Button1Click(Sender: TObject);
var
dset: TNumSet;
begin
// initialize
dset := [LBase..UBase];
IterCnt := 0;
// call the recursive proc
DigRecurse(dset, 0);
end;

again be carefull about the stack,
regards,
igor
0
Author Commented:
I evaluate the code and it works for all combinations, so also 1 2 2 2 2 2.
That is not the meaning.

I want that the second number is bigger than the first, third number bigger than second, fourth number bigger than third, fifth number bigger than fourth and sixt number bigger than fifth.

and this gives me 593.775 combinations that i want to put in a DB.

and on this combinations i want to put some query's
0
Commented:
ok got it,
simple just change the following:

1-
Seq: array[0..DigitCnt - 1] of Integer;
to
Seq: array[-1..DigitCnt - 1] of Integer;

2-
for I := LBase] to UBase do
to
for I := seq[digit-1] to UBase do

3-
// initialize
dset := [LBase..UBase];
to
// initialize
seq[-1] := LBase;
dset := [LBase..UBase];

I hope this makes it ok
igor
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.