Java application for finding permutations efficiently I am an eighth grader with a school project of creating and application in Java that returns the total permutations of two given numbers.  It needs to be light and efficient, for later deployment on a website.  I was wondering if there was any way I could improve the code so it would be more efficient.

class Factorials {
    public static void main(String[] args){
        int n = 8;
        long factorialN = n;
        for (int ForN = n; ForN &lt;= n &amp;&amp; ForN &gt; 1; ForN--) {
            factorialN = factorialN * ForN;
        }
        factorialN = factorialN / n;
        System.out.println(factorialN);

        int r = 6;
        int rMinus1 = r - 1;
        long factorialR = rMinus1;
        for (int ForR = rMinus1; ForR &lt;= rMinus1 &amp;&amp; ForR &gt; 1; ForR--) {
            factorialR = factorialR * ForR;
        }
        factorialR = factorialR / rMinus1;
        System.out.println(factorialR);

        int nMr = n - r;
        System.out.println(nMr);
        long factorialNmR = nMr;
        if (nMr == 2) {
            factorialNmR = 2;
        }
        else if (nMr &lt;= 1){
            factorialNmR = 1;
        }
        else if (nMr &gt; 2) {
            for (int FornMr = nMr; FornMr &lt;= nMr &amp;&amp; FornMr &gt; 1; FornMr--) {
                factorialNmR = factorialNmR * FornMr;
            }
            factorialNmR = factorialNmR / nMr;
            System.out.println(factorialNmR);
        }

        long permutations = factorialN;
        System.out.println(permutations);
        permutations = permutations / factorialNmR;
        System.out.println(permutations);
    }
}
First of all, please write a single function instead of expanding what is essentially the same code three times!

long factorialN = n;
for (int ForN = n; ForN &lt;= n &amp;&amp; ForN &gt; 1; ForN--) {
    factorialN = factorialN * ForN;
}
factorialN = factorialN / n;


Also, if your for-loop counter will start at N and only decrement, you do not need to check in every iteration whether it is less than or equal to N; it is guaranteed to be. You only need to check if it is greater than 1. But that incurs no performance penalty, since the Java compiler is clever enough to disregard your superfluous check.

Now, if you want a faster factorial implementation, you can use a recursive one, like the one described in this page. It is in C#, but you should have no problem converting it to Java. The only thing is, you need to gain knowledge of what recursion really is before trying to pass this as a piece of code you understand.

Or, you could use something far more advanced, like the asymptotic prime factorization algorithm described on this page, though you will have a very hard time convincing your teacher that you, an 8th grader, have the slightest clue as to how this algorithm works, and why, so better stay away from it!