0

import com.google.common.math.*;
class Main {
    public static void main (String[] args) {
        System.out.println(IntMath.factorial(5));
    }
}

This is my code. I was just checking if could find any builtin function for factorial.

rainer
  • 3,295
  • 5
  • 34
  • 50
  • Please include a [mre]. For one thing, we need to know what your classpath looks like. – pcarter Apr 17 '20 at 15:54
  • 5
    "i was just checking if could find any builtin function for factorial" `com.google.common.math` isn't built-in. You'd need to include Guava on your classpath. – Andy Turner Apr 17 '20 at 15:56
  • ...for that matter, what build and dependency-management tools are you using? Maven, or anything like it? – Charles Duffy Apr 17 '20 at 15:57
  • (There isn't a builtin function for factorial in Java SE.) – Stephen C Apr 17 '20 at 15:58
  • Does this answer your question? [how to include libraries in java without using an IDE](https://stackoverflow.com/questions/5112607/how-to-include-libraries-in-java-without-using-an-ide) – Charles Duffy Apr 17 '20 at 15:58
  • The built-in way: `IntStream.closedRange(1, 5).reduce((a, b) -> a * b))` (doesn't handle overflow, ofc). – Andy Turner Apr 17 '20 at 15:59
  • Yes. Overflow is probably the reason that there isn't a built-in function too. (20! is the largest factorial value that can be represented as a `long`.) Which means that if you are going to do computations of (say) power series involving factorials, you will either need to use `BigDecimal` or restructure the computation. Numerical Analysis time ... – Stephen C Apr 17 '20 at 16:08

0 Answers0