import java.math.BigInteger;
public class Factorial {
/* returns the n factorial, or -1 if it does not exist; works up to 20 */
public static long factorial (long n) {
if (n > 1) return n * factorial(n - 1);
else if (n >= 0) return 1;
else return -1;
}
/* returns the n factorial, or -1 if it does not exist (in the form of BigInteger) */
public static BigInteger bigFactorial (int n) {
if (n > 1) return new BigInteger(String.valueOf(new BigInteger(String.valueOf(n)).multiply(bigFactorial(n - 1))));
else if (n >= 0) return new BigInteger("1");
else return new BigInteger("-1");
}
}
DOWNLOAD
Created: February 12, 2014
Completed in full by: Michael Yaworski