BigIntegerMath factorial() function | Guava | Java
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.
Syntax:
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.
Note:
- 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:
Example 1:
// Java code to show implementation of // factorial() method of Guava's BigIntegerMath class import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { int n1 = 10; // Using factorial(int n) method of // Guava's BigIntegerMath class BigInteger ans1 = BigIntegerMath.factorial(n1); System.out.println("Factorial of " + n1 + " is: " + ans1); int n2 = 12; // Using factorial(int n) method of // Guava's BigIntegerMath class BigInteger ans2 = BigIntegerMath.factorial(n2); System.out.println("Factorial of " + n2 + " is: " + ans2); } } |
Factorial of 10 is: 3628800 Factorial of 12 is: 479001600
Example 2:
// Java code to show implementation of // factorial() method of Guava's BigIntegerMath class import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { try { int n1 = -5; // Using factorial(int n) method of // Guava's BigIntegerMath class // This should throw "IllegalArgumentException" // as n < 0 BigInteger ans1 = BigIntegerMath.factorial(n1); System.out.println("Factorial of " + n1 + " is: " + ans1); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.IllegalArgumentException: n (-5) must be >= 0
Recommended Posts:
- BigIntegerMath floorPowerOfTwo() function | Guava | Java
- BigIntegerMath isPowerOfTwo() function | Guava | Java
- BigIntegerMath log10() function | Guava | Java
- BigIntegerMath divide() function | Guava | Java
- BigIntegerMath ceilingPowerOfTwo() function | Guava | Java
- BigIntegerMath binomial() function | Guava | Java
- BigIntegerMath sqrt() function | Guava | Java
- BigIntegerMath log2() function | Guava | Java
- BigIntegerMath Class | Guava | Java
- Java Guava | Longs.factorial(int n) method with Examples
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Ints min() function | Guava | Java
- Ints max() function | Guava | Java
- Ints contains() function | Guava | Java
- Ints join() function | Guava | Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



