Hi

some help please

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

some help please

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

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.ProcessMessage

end;

end;

end;

// and use it as follows

procedure TForm1.Button1Click(Sender

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

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

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