If I have "n" numbers, how to list it in every possible combination. Example
for 3 numbers:
3, 7, 21
1. combination is: 3,7,21
2. combination is:7,3,21
3. combination is: 21,3,7
rossum - 10 Mar 2008 12:56 GMT
>If I have "n" numbers, how to list it in every possible combination. Example
>for 3 numbers:
>3, 7, 21
>1. combination is: 3,7,21
>2. combination is:7,3,21
>3. combination is: 21,3,7
You are asking about "permutations" here, not "combinations". A
permutation is the same items in a different order, as you have shown.
A combination is a different selection from the group of items.
Combinations would be:
1: 3, 7
2: 3, 21
3: 21
4: 3, 7, 21
etc.
Try googling for "permutation algorithm"
rossum
Markus Tazl - 10 Mar 2008 19:30 GMT
> If I have "n" numbers, how to list it in every possible combination. Example
> for 3 numbers:
> 3, 7, 21
> 1. combination is: 3,7,21
> 2. combination is:7,3,21
> 3. combination is: 21,3,7
As far as i know Robert Sedgewick had an article on permutation on his
website,
maybe searching http://www.cs.princeton.edu/~rs/ might be helpful.
best regards
Markus Tazl
Roedy Green - 11 Mar 2008 02:11 GMT
>If I have "n" numbers, how to list it in every possible combination. Example
>for 3 numbers:
>3, 7, 21
>1. combination is: 3,7,21
>2. combination is:7,3,21
>3. combination is: 21,3,7
You have combinations and permutations confused.
See http://mindprod.com/jgloss/permutation.html
http://mindprod.com/jgloss/combination.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Stefan Ram - 11 Mar 2008 03:01 GMT
>If I have "n" numbers, how to list it in every possible combination. Example
>for 3 numbers:
>3, 7, 21
>1. combination is: 3,7,21
>2. combination is:7,3,21
>3. combination is: 21,3,7
As others already have explained, these are not combinations,
but permutations.
Permutations are a part of the library »ram.jar«:
public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.Object[] hand = new java.lang.Object[]{ "T", "H", "E" };
for
( final java.lang.Object[] p :
new de.dclj.ram.system.iteration.Permutations( hand ))
java.lang.System.out.print( java.util.Arrays.toString( p )); }}
[T, H, E][T, E, H][H, T, E][H, E, T][E, T, H][E, H, T]
http://www.purl.org/stefan_ram/pub/ram-jar