-2

this might be a stupid question with a simple answer but I can't for my life figure it out.. :p

So if I have a variable n = 2 for example I want a list of all ways you can combinate the numbers that's less than or equal to n

The result for n = 2 would then be:
012
021
102
120
201
210

Thanks for any help :)

Chris Sharp
  • 2,005
  • 1
  • 18
  • 32
  • You need a myarray. First generate it simple. For Int x= 0, x< n, x++. So you got a [0,1] array. The second array will be your cast values and permute add your n and shuffle but only for unique values. I am on mobile but I think you got the idea behind. Not exactly in java, but try to catch the hint :) and go with others suggestion. – ares777 Mar 02 '17 at 19:51

2 Answers2

1

This is a math question but you're trying to calculate the permutations of 0 - n.

To calculate permutations use the formula nPk, where, in your case, k is the number which was chosen and n = k + 1. You want to take the factorial of n and divide that by the factorial of n - k. ==> n! / (n - k)! In your example that would be

3!/(3 - 2)! 
3!/1! 
3 * 2 * 1 / 1 = 6 

The link below goes into more detail. http://www.mathwords.com/p/permutation_formula.htm

Chris Sharp
  • 2,005
  • 1
  • 18
  • 32
  • This is count of, not what OP asked. But , well... some formulas are indeed made for people trying to learn. – ares777 Mar 02 '17 at 19:56
  • It's exactly what was asked for. All of the combinations of digits between 0 and n. Even the example is a list of permutations. In the formula, n will always be zero but the formula still works the same way. – Chris Sharp Mar 02 '17 at 20:01
  • Even in your comment answer you prescribe permutations. The question doesn't even ask for code. – Chris Sharp Mar 02 '17 at 20:05
  • Meanwhile, follow the link other posters already -> I like playing my old Korg Polysix also. – ares777 Mar 02 '17 at 20:16
  • I have edited the answer so that there is enough information that an algorithm could be derived by the example and the link merely supports it. – Chris Sharp Mar 02 '17 at 21:28
  • The OP apparently asked for the permutations of, to use a different letter, `N + 1` objects taken `N + 1` at a time, where `N` is the given number (`2` in their example). So for `N == 2`, that's `3` objects taken `3` at a time, not `2` at a time. – Lew Bloch Mar 02 '17 at 22:17
  • He wants all permutations >= the digits from 0 to n. I just realized that I didn't answer the question but gave him the number of permutations for any given n. However, I did switch the letters in my answer to conform to the nPk formula. – Chris Sharp Mar 02 '17 at 22:30
0

public class NumberCombination {

public static void combination(int[] num, int x){//x is used to tell from which position in array permutations are needed to be done.
    if(x!=num.length){
        for(int i=x;i<num.length;i++){
            int temp = num[i];
            num[i] = num[x];
            num[x] = temp;
            combination(num,x+1);
            temp = num[i];
            num[i] = num[x];
            num[x] = temp; 
        }
    }
    else{
        for(int i=0;i<num.length;i++)
            System.out.print(num[i]);
        System.out.println();
    }
}

public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the number:");
    int n = Integer.parseInt(br.readLine());
    int[] num = new int[n+1];
    for(int i=0;i<=n;i++)
        num[i] = i;
    combination(num, 0);

}

}