Java Guava | Longs.factorial(int n) method with Examples

The factorial(int n) method of Guava’s LongMath Class returns the product of the first n positive integers, which is n!.

Syntax :

public static long factorial(int n)

Parameter: The method accepts only one parameter n which is of integer type and is to be used to find the factorial.

Return Value: This method return following values:

  • This method returns 1 if n is 0.
  • This method returns product of the first n positive integers if the result fits in a long.
  • This method returns Long.MAX_VALUE if the result does not fit in a long.

Exceptions: The method factorial(int n) throws IllegalArgumentException if n is negative.

Below programs illustrate the use of LongMath.factorial() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// factorial(int n) method of Guava's
// LongMath Class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
        int n1 = 10;
  
        // Using factorial(int n) method of
        // Guava's LongMath class
        long ans1 = LongMath.factorial(n1);
  
        System.out.println("factorial of "
                           + n1 + " is : "
                           + ans1);
  
        int n2 = 12;
  
        // Using factorial(int n) method of
        // Guava's LongMath class
        long ans2 = LongMath.factorial(n2);
  
        System.out.println("factorial of "
                           + n2 + " is : "
                           + ans2);
    }
}

chevron_right


Output:

factorial of 10 is : 3628800
factorial of 12 is : 479001600

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// factorial(int n) method of Guava's
// LongMath Class
  
import java.math.RoundingMode;
import com.google.common.math.LongMath;
  
class GFG {
  
    static long findFact(int n)
    {
        try {
            // Using factorial(int n) method of
            // Guava's LongMath class
            // This should throw "IllegalArgumentException"
            // as n < 0
            long ans = LongMath.factorial(n);
  
            // Return the answer
            return ans;
        }
        catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = -5;
  
        try {
  
            // Function calling
            findFact(n);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.IllegalArgumentException: n (-5) must be >= 0

Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#factorial-int-



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.