I\'ve been using this factorial program for Java:
public static long factorial(int a) {
if(a<1) {
return 1;
}
long result=1;
long
Another approach that's less naive and works for non-integer numbers is to use the natural log of the gamma function.
http://www.iro.umontreal.ca/~simardr/ssj/doc/html/umontreal/iro/lecuyer/util/Num.html
If you must persist in using this implementation, I'd recommend that you look into memoization. Why keep recalculating values? Once you have one, hang onto it and just hand it out on repeat requests.