# 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 <= n && ForN > 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 <= rMinus1 && ForR > 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 <= 1){
factorialNmR = 1;
}
else if (nMr > 2) {
for (int FornMr = nMr; FornMr <= nMr && FornMr > 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 <= n && ForN > 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!

• My only question is, how would I create only one function? And also, my school requires that for each project, I need a live resource. Basically, I need someone to help me that isn't a website. Could I consider you my live resource for this project? Dec 19, 2011 at 15:49
• Possibly, (if it does not involve anything more than communicating via email,) but first I want to know if you are any good. Because it is one thing to help someone do their work and another to do their work for them. So, I edited my answer to show you the piece of code which is triplicate. Can you not extract that code into a separate function and call that function three times from within your main function? I mean, if not, then you need to learn the very basics of programming, instead of worrying about factorials, let alone of efficient means of computing factorials. Dec 19, 2011 at 16:24
• Communicating via email is fine, and for the programming sufficiency, I believe I could probably do it, but I would need some help, not a lot, but some nonetheless. Dec 19, 2011 at 17:30
• The first part of my email address is 'michael'. The last part (the part after the 'at' sign) is listed in my profile as my website. I will need you to do what I asked in my previous comment before I become your live resource. It has nothing to do with efficiency, so it is not answering your question. It just tells me that you know a bit of what you are doing so you are not just trying to find someone to do your homework for you. Also I would like you to send me whatever information you can explaining to me what your school considers a live resource. Dec 19, 2011 at 19:38
• don't forget, stackexchange has a chatroom too for another medium of communication. Dec 20, 2011 at 1:02