Monday, December 15, 2014
BigInteger to the rescue
Java's Math library provides BigInteger that can hold any kind of largest number (no idea how BigInteger breaks the barrier of 64 bit and I am looking for an answer)
import java.math.*;
class bigfactorial {
public static BigInteger compute(int num)
{
if(num < 0)
{ throw new IllegalArgumentException("negative number passed for factorial computation");}
if(num < 2)
{return BigInteger.ONE;}
BigInteger factorial = BigInteger.ONE;
while(num > 1)
{ factorial = factorial.multiply(BigInteger.valueOf(num--)); }
return factorial;
}
}
public class biginteger {
public static void main(String args[]){
for (int i=2; i<25;i++)
System.out.println("The " + i + " ! is: "
+ bigfactorial.compute(i));
}
}
Labels:
biginteger,
data types,
factorial,
JAVA
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment