Gets the factorial of a large number - Java java.lang

Java examples for java.lang:Math Number

Description

Gets the factorial of a large number

Demo Code


//package com.java2s;

import java.math.BigInteger;

public class Main {
    /**//from   www .j av a 2s .c o  m
     * Gets the factorial of a large number
     *
     * @param num Number to start at
     * @return The factorial
     */
    public static BigInteger bigFactorial(BigInteger num) {
        if (num.equals(BigInteger.ZERO))
            return BigInteger.ONE;
        if (num.equals(BigInteger.valueOf(2L))
                || num.equals(BigInteger.valueOf(1L))) {
            return num;
        } else {
            return num.multiply(bigFactorial(num.subtract(BigInteger.ONE)));
        }
    }
}

Related Tutorials