Search code examples
algorithmmathfactorial

Factorial of factorial of a number


How can the factorial of a factorial of a number be efficiently computed.
Example:For 3 => (3!)! = (6)! = 720
The brute force way would be to simply call factorial twice using a simple for loop but can it be done better.

for(i=1;i<=n;i++) 
   fact=fact*i; 

Edit: Need the result as ((n!)!)MOD 10^m, where m is an integer and 0<=m<=19


Solution

  • Note that result is 0 for n >=5 (5!!=120! has more than 19 zeros at the end), and result for smaller values it is easy to calculate.