How many possible groups in a serial of number, I need all of the goups

Hi,

Just don't know how whether it can be written a program in Java that performs such a logic. Seems very complicated because I need all of the possible groups

Here's a simple example.

1 2 3 4 5 6 7

in these seven numbers

I just choose four of them. Can I write a jave program to list out all of the possible groups if I choose four of them?

For example

1 2 3 4 can be a group

2 3 5 6 also can be a group

......

I need to list out all of the possible groups and they should be order from small to large.

If I didn't do a wrong calculation, it may be 840 possible groups totally.

I have no idea about how to write such a program to fulfill such a logic in Java. Is it possible???

Thanks.

Re: How many possible groups in a serial of number, I need all of the goups

Is, say, 1 2 3 4 different from 4 3 2 1? (the same numbers but in a different order).

kind regards,

Jos

Re: How many possible groups in a serial of number, I need all of the goups

They are the same. Thanks for your kindly remind. So there maybe less than 840 possible groups since some of them are just in different order.

for example,

1 2 3 4

4 3 2 1

2 1 3 4

are the same. the result should be 1 2 3 4, I want them to be ordered.

Thanks.

Quote:

Originally Posted by

**JosAH** Is, say, 1 2 3 4 different from 4 3 2 1? (the same numbers but in a different order).

kind regards,

Jos

Re: How many possible groups in a serial of number, I need all of the goups

So you want all C(n,m) combinations? (in your example C(7,4) == 7!/(4!*3!) == 35); have a look at the following method; given a 'group' stored in array 'c', the method generates the next group, given the size 'n' of the elements to pick from:

Code:

` public static boolean combine(int[] c, int n) {`

for (int i= c.length; --i >= 0;) {

if (++c[i] <= n-(c.length-i)) {

for (; ++i < c.length; )

c[i]= c[i-1]+1;

return true;

}

}

return false;

}

In your example, create a group 0, 1, 2, 3 and a size n= 7 and run the above method over and over again until it returns false; each time it ran it has generated a next 'group'.

kind regards,

Jos

Re: How many possible groups in a serial of number, I need all of the goups

So greate that your mathematics is quite good. Your calculation is right and that's what I want.

I'll investigate the code and give you a reply if the program is OK.

Many thanks~

Quote:

Originally Posted by

**JosAH** So you want all C(n,m) combinations? (in your example C(7,4) == 7!/(4!*3!) == 35); have a look at the following method; given a 'group' stored in array 'c', the method generates the next group, given the size 'n' of the elements to pick from:

Code:

` public static boolean combine(int[] c, int n) {`

for (int i= c.length; --i >= 0;) {

if (++c[i] <= n-(c.length-i)) {

for (; ++i < c.length; )

c[i]= c[i-1]+1;

return true;

}

}

return false;

}

In your example, create a group 0, 1, 2, 3 and a size n= 7 and run the above method over and over again until it returns false; each time it ran it has generated a next 'group'.

kind regards,

Jos