A programmer's tale

Thursday, July 20, 2006

Factorial of 100: Java Way

Hi,
After a long time (I think, it's long, but a galaxy says it's very short, some Leptons may say it's very long time, kidding) I'm back with a small java program that can calculate the factorial of large numbers such that 25, 30 or even 100:

public class FactorialBig {

/**
* @param args
*/
public static void main(String[] args) {
long l = 50;

System.out.println("--------BigInteger factorial-------");
System.out.println("Factorial of " + l + " = " + factorial(l));
}

public static BigInteger factorial(long number) {
return fact(BigInteger.valueOf(number));
}

private static BigInteger fact(BigInteger n) {
if (n.compareTo(BigInteger.ZERO) == 0)
return BigInteger.ONE;
else
return n.multiply(fact(n.subtract(BigInteger.ONE)));
}
}

The out put of this program is:

--------BigInteger factorial-------
Factorial of 50 = 30414093201713378043612608166064768844377641568960512000000000000