23
Jan
2020

Hackerrank Extra Long Factorials (Java)

Java :

static void extraLongFactorials(int n) {
        BigInteger factorial = new BigInteger("1");

        for(int i = 1; i <= n; i++){
            factorial = factorial.multiply(BigInteger.valueOf(i));
        }

        System.out.println(factorial);
}

Explanation:

This question is so much easier done in Java, since Java has the BigInteger class that supports huge numbers. Another language that supports these big numbers is Ruby.

The for loop goes from i = 1 to n, such that the factorial multiplies by i in each loop until it is equal to n.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *