Tag Archives: factorial

Finding factorial of a large number

In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5 ! = 5  \times  4  \times  3  \times  2  \times  1 = 120  \

0! is a special case that is explicitly defined to be 1.

When asked to write a program to compute factorial of a number, say n, we implement this definition into lines of code using a counter which iterates over the values from 1 to n and keep updating the result by multiplying the previous result with current value of counter. There is nothing wrong with this logic but the program fails to give correct results for 20! or higher. This is due to the limitations of the computing resources as the result becomes so large that it is not possible to store them in a 32-bit or 64-bit word.

Hence, to compute factorial of large numbers, we need to define our own logic.

Hint: Implement paper and pen method of multiplication without using Integer type variables.

Solution