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
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
2 Comments:
Update: Look, this is not the most optimised code. Optimization can be achieved through Dynamic Programming
By gRoy(), at 1:54 PM
how does viagra work cheapest uk supplier viagra cialis v s viagra buy cheap viagra online how does viagra work viagra side affects natural herbs used as viagra viagra online cheap viagra and cialis viagra oral jelly viagra covered by insurance viagra generic soft tab too much viagra free sample viagra
By Anonymous, at 10:55 AM
Post a Comment
<< Home