Search code examples
javaapache-commonsfactorial

How to get the factorial value


I want to get the factorial value of a number. I read in a wiki that I can expect the result of 5! to be 5x4x3x2x1 = 120. Now how can I get that in Java?

BigFraction g = new BigFraction(5);
System.out.println(g.getNumerator());

This prints just 5.

In the end i want to calculate combinations in a network:

network

Which has the following formula:

example from wiki


Solution

  • By the way, why not just use the plain old formula:

    x=n*(n-1)/2
    

    Where n is the number of vertices.

    For this simple task, you don't have to use a computationally intensive function as a factorial...