Permutations Finder Help Number 2 Previous question:

Java application for finding permutations efficiently>

I have changed quite a bit of code, and need some more reviews.

class Permutations {

    static long factorial(int num){
        long factorial = num;
        for (int forBlockvar = num; forBlockvar &gt; 1; forBlockvar--) {
            factorial = factorial * forBlockvar;
        }
        return factorial / num;
    }

    public static void main(String[] args){
        long FactNmR;
        int n = 8;
        int num = n;
        int r = 6;
        int nMr = n - r;

        long FactN = factorial(num);

        if (nMr == 2) {
            FactNmR = 2;
        }
        else if (nMr &lt;= 1){
            FactNmR = 1;
        }
        else if (nMr &gt;= 2) {
            num = nMr;
            FactNmR = factorial(num);
        }

        long permutations = FactN;

        permutations = permutations / FactNmR;

        System.out.println(permutations);
    }
}
Code comment:

static long factorial(int num){
    long factorial = num;
    for (int forBlockvar = num; forBlockvar &gt; 1; forBlockvar--) {
        factorial = factorial * forBlockvar;
    }
    return factorial / num;
}


You are starting your factorial = num, then multiplying it by num in the first iteration of the loop, then dividing by num at the end.  Starting with factorial = 1, or starting  forBlockvar = num - 1 (I prefer the second) will remove the need to divide at the end.  You can also return 1 if the input is less than 2.

static long factorial(int num){
    if(num &lt; 2) {
        return 1;
    }
    long factorial = num;
    for (int forBlockvar = num - 1; forBlockvar &gt; 1; forBlockvar--) {
        factorial = factorial * forBlockvar;
    }
    return factorial;
}


You could also implement this function recursively.

static long factorial(int num){
    if(num &lt; 2) {
        return 1;
    }
    return num * factorial(num - 1);
}


General:

What do you mean by finding the permutations of 2 numbers?  There are 2 permutations of 2 numbers [x, y]: [x, y] and [y, x].

EDIT

I think I know what you are trying to do now... You want to know how many permutations are available by selecting r objects out of a total of n.

The basic formula for that is n! / (n-r)!.  However, this means calculating the factorial of (n-r) twice (in our example, we have 8*7*6*5*4*3*2*1 / 2*1, but with a lower r the overlap will be greater).

Instead of calculating the complete factorial for both numerator and denominator, there is a way to calculate the value of the numerator such that the denominator is always going to be 1.  That means we eliminate one factorial calculation, and reduce the other.  I'm going to let you think about it a bit before just giving the answer.

EDIT 2

Since you have figured out what I was hinting at, I'll explain it further.

n! can be represented as n*(n-1)*(n-2)*(n-3)...(2)*(1).  When r is less than n and greater than 0, n! can be then be represented as n*(n-1)*(n-2)...(n-r+1)*(n-r)*(n-r-1)*(n-r-2)...(2)*(1).

(n-r)! is (n-r)*(n-r-1)*(n-r-2)...(2)*(1).

Therefore, our numerator is n*(n-1)*(n-2)...(n-r+1)*(n-r)*(n-r-1)*(n-r-2)...(2)*(1), and our denominator is (n-r)*(n-r-1)*(n-r-2)...(2)*(1).  Cancelling like terms, we have n!/(n-r)! = n*(n-1)*(n-2)...(n-r+1).

static long permute(int totalItems, int numToPick) {
    long permutations = totalItems;
    while (--numToPick &gt; 0) {
        permutations *= --totalItems;
    }
    return permutations;
}


You should ensure that 0 &lt; numToPick &lt;= totalItems, and totalItems &gt; 0.