The method factorial(int n) of Guava’s BigIntegerMath class is used to find the factorial of the given number. It returns n!, that is, the product of the first n positive integers.
public static BigInteger factorial(int n)
Parameters: This method takes the number n as parameter whose factorial is to be found.
Return Value: This method returns the factorial of the given number n.
Exceptions: This method throws IllegalArgumentException if n < 0.
- The method returns 1 if n == 0.
- The result takes O(n log n) space, so use cautiously.
- This uses an efficient binary recursive algorithm to compute the factorial with balanced multiplies.
Below examples illustrates the BifIntegerMath.factorial() method:
Factorial of 10 is: 3628800 Factorial of 12 is: 479001600
Exception: java.lang.IllegalArgumentException: n (-5) must be >= 0
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.