package ints;
public class Factorial{
public static int fact(int n){
if (n < 0)
throw new ArithmeticException();
else {
int out = 1;
for (;n > 0;n--)
out = out*n;
return out;
}
}
public static int factRec(int n){
if (n < 0)
throw new ArithmeticException();
else if (n == 0) return 1;
else return n*factRec(n-1);
}
public static int multiFact(int n,int k){
if ((n < 0) || (k < 1)) throw new ArithmeticException();
else if ((0 <= n) && (n < k)) return 1;
else return n*multiFact(n-k,k);
}
public static int quadFact(int n){
if (n < 0) throw new ArithmeticException();
else return fact(2*n)/fact(n);
}
public static int superFact(int n){
if (n < 0) throw new ArithmeticException();
else if (n == 0) return 1;
else return fact(n)*superFact(n-1);
}
public static int hyperFact(int n){
if (n < 0) throw new ArithmeticException();
else if (n == 0) return 1;
else return exp(n,n)*hyperFact(n-1);
}
public static int alternatingFact(int n){
if (n < 0) throw new ArithmeticException();
else if (n == 0) return 1;
else return fact(n)- alternatingFact(n-1);
}
public static int exp(int base, int exponent){
int result = 1;
int i = exponent;
while (i > 0){
result *= base;
i--;
}
return result;
}
/*
public static void main(String[] args){
System.out.println(hyperFact(4));
}
*/
}
|