import java.math.BigInteger; /** Computes an exact factorial, using a BigInteger. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class Factorial { public static void main(String[] args) { for(int i=1; i<=256; i*=2) { System.out.println(i + "!=" + factorial(i)); } } public static BigInteger factorial(int n) { if (n <= 1) { return(new BigInteger("1")); } else { BigInteger bigN = new BigInteger(String.valueOf(n)); return(bigN.multiply(factorial(n - 1))); } } }
Factorial.java Computes an exact factorial, n!, using a BigInteger
Permanent link to this article: http://bangla.sitestree.com/factorial-java-computes-an-exact-factorial-n-using-a-biginteger/