Results 1 to 5 of 5
  1. #1
    Calvin_gz is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Calvin_gz is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default 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 View Post
    Is, say, 1 2 3 4 different from 4 3 2 1? (the same numbers but in a different order).

    kind regards,

    Jos

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default 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:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Calvin_gz is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default 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 View Post
    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:

    Java 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

Similar Threads

  1. Serial number of exernal hard disk/Thumbdrive
    By manucng in forum Networking
    Replies: 13
    Last Post: 11-14-2011, 08:09 AM
  2. Serial communication via ethernet-serial hub
    By Idiodyssey in forum New To Java
    Replies: 0
    Last Post: 05-04-2011, 02:23 PM
  3. Replies: 1
    Last Post: 07-30-2010, 08:29 AM
  4. Help me to get RAM serial number
    By Ajitha in forum New To Java
    Replies: 5
    Last Post: 06-05-2010, 02:30 PM
  5. Replies: 1
    Last Post: 08-07-2009, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •