java, factorial calculation

Hello. how to get a full number that goes beyond the long type, and therefore gives a wrong answer. most likely, there will be another number that will consist of two types of long, but only in order to expand the registers. I don't understand how to do it.

public class CalculateFactorial{
public static void main(String[] args){
    factorial fact = new factorial();
    System.out.println(fact.factoring(23));
}
static class factorial{
    long result = 1;
    public long factoring(long x){
        for(long i = 1; i<=x; i++){
            result *= i;
        }
        return result;
    }
}

}

 0
Author: Pashka Fincler, 2017-10-14

1 answers

You don't need to do it manually, there is a Biginteger type

public static BigInteger factorial(BigInteger n) {
    BigInteger result = BigInteger.ONE;

    while (!n.equals(BigInteger.ZERO)) {
        result = result.multiply(n);
        n = n.subtract(BigInteger.ONE);
    }

    return result;
}
 4
Author: HasmikGaryaka, 2017-10-14 10:00:24