Product of first N factorials

Given a number N. Find the product of first N factorials modulo 1000000007.

Constraints: 1 ≤ N ≤ 1e6

Examples:

Input : 3
Output : 12
Explanation: 1! * 2! * 3! = 12 mod (1e9 + 7) = 12

Input : 5
Output : 34560

Prerequisites: Modular Multiplication

Approach: The basic idea behind solving this problem is to just consider the problem of overflow during multiplication of such large numbers i.e. factorials. Hence, it needs to be addressed by multiplying recursively to overcome the difficulty of overflow. Moreover, we have to take modulus at every step while computing factorials iteratively and modular multiplication.



facti = facti-1 * i
where facti is the factorial of ith number

prodi = prodi-1 * facti
where prodi is the product of first i factorials

To find product of two large numbers under modulo, we use same approach as exponentiation under modulo.. In the multiplication function, we use + instead of *.

Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the
// product of first N factorials
#include <bits/stdc++.h>
  
using namespace std;
  
// To compute (a * b) % MOD
long long int mulmod(long long int a, long long int b, 
                                    long long int mod)
{
    long long int res = 0; // Initialize result
    a = a % mod;
    while (b > 0) {
  
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;
  
        // Multiply 'a' with 2
        a = (a * 2) % mod;
  
        // Divide b by 2
        b /= 2;
    }
  
    // Return result
    return res % mod;
}
  
// This function computes factorials and
// product by using above function i.e.
// modular multiplication
long long int findProduct(long long int N)
{
    // Intialize product and fact with 1
    long long int product = 1, fact = 1;
    long long int MOD = 1e9 + 7;
    for (int i = 1; i <= N; i++) {
  
        // ith factorial
        fact = mulmod(fact, i, MOD);
  
        // product of first i factorials
        product = mulmod(product, fact, MOD);
  
        // If at any iteration, product becomes
        // divisible by MOD, simply return 0;
        if (product == 0)
            return 0;
    }
    return product;
}
  
// Driver Code to Test above functions
int main()
{
    long long int N = 3;
    cout << findProduct(N) << endl;
  
    N = 5;
    cout << findProduct(N) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the
// product of first N factorials
  
class GFG{
// To compute (a * b) % MOD
static double mulmod(long a, long b, 
                                    long mod)
{
    long res = 0; // Initialize result
    a = a % mod;
    while (b > 0) {
  
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;
  
        // Multiply 'a' with 2
        a = (a * 2) % mod;
  
        // Divide b by 2
        b /= 2;
    }
  
    // Return result
    return res % mod;
}
  
// This function computes factorials and
// product by using above function i.e.
// modular multiplication
static long findProduct(long N)
{
    // Intialize product and fact with 1
    long product = 1, fact = 1;
    long MOD = (long)(1e9 + 7);
    for (int i = 1; i <= N; i++) {
  
        // ith factorial
        fact = (long)mulmod(fact, i, MOD);
  
        // product of first i factorials
        product = (long)mulmod(product, fact, MOD);
  
        // If at any iteration, product becomes
        // divisible by MOD, simply return 0;
        if (product == 0)
            return 0;
    }
    return product;
}
  
// Driver Code to Test above functions
public static void main(String[] args)
{
    long N = 3;
    System.out.println(findProduct(N));
  
    N = 5;
    System.out.println(findProduct(N));
  
}
}
// this Code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to find the
# product of first N factorials
  
# To compute (a * b) % MOD
def mulmod(a, b, mod):
    res = 0 # Initialize result
    a = a % mod
    while (b > 0):
  
        # If b is odd, add 'a' to result
        if (b % 2 == 1):
            res = (res + a) % mod
  
        # Multiply 'a' with 2
        a = (a * 2) % mod
  
        # Divide b by 2
        b //= 2
  
    # Return result
    return res % mod
  
# This function computes factorials and
# product by using above function i.e.
# modular multiplication
def findProduct(N):
    # Intialize product and fact with 1
    product = 1; fact = 1
    MOD = 1e9 + 7
    for i in range(1, N+1):
  
        # ith factorial
        fact = mulmod(fact, i, MOD)
  
        # product of first i factorials
        product = mulmod(product, fact, MOD)
  
        # If at any iteration, product becomes
        # divisible by MOD, simply return 0
        if not product:
            return 0
    return int(product)
  
# Driver Code to Test above functions
N = 3
print(findProduct(N))
N = 5
print(findProduct(N))
  
# This code is contributed by Ansu Kumari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C#  Program to find the
// product of first N factorials
  
using System;
  
public class GFG{
    // To compute (a * b) % MOD
static double mulmod(long a, long b, 
                                    long mod)
{
    long res = 0; // Initialize result
    a = a % mod;
    while (b > 0) {
  
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;
  
        // Multiply 'a' with 2
        a = (a * 2) % mod;
  
        // Divide b by 2
        b /= 2;
    }
  
    // Return result
    return res % mod;
}
  
// This function computes factorials and
// product by using above function i.e.
// modular multiplication
static long findProduct(long N)
{
    // Intialize product and fact with 1
    long product = 1, fact = 1;
    long MOD = (long)(1e9 + 7);
    for (int i = 1; i <= N; i++) {
  
        // ith factorial
        fact = (long)mulmod(fact, i, MOD);
  
        // product of first i factorials
        product = (long)mulmod(product, fact, MOD);
  
        // If at any iteration, product becomes
        // divisible by MOD, simply return 0;
        if (product == 0)
            return 0;
    }
    return product;
}
  
// Driver Code to Test above functions
    static public void Main (){
        long N = 3;
        Console.WriteLine(findProduct(N));
        N = 5;
        Console.WriteLine(findProduct(N));
  
}
}
//This Code is contributed by ajit.

chevron_right


PHP

0)
{

// If b is odd, add ‘a’ to result
if ($b % 2 == 1)
$res = ($res + $a) % $mod;

// Multiply ‘a’ with 2
$a = ($a * 2) % $mod;

// Divide b by 2
$b /= 2;
}

// Return result
return $res % $mod;
}

// This function computes factorials and
// product by using above function i.e.
// modular multiplication
function findProduct($N)
{
// Intialize product and fact with 1
$product = 1;
$fact = 1;
$MOD = 1000000000;
for ($i = 1; $i <= $N; $i++) { // ith factorial $fact = mulmod($fact, $i, $MOD); // product of first i factorials $product = mulmod($product, $fact, $MOD); // If at any iteration, product becomes // divisible by MOD, simply return 0; if ($product == 0) return 0; } return $product; } // Driver Code $N = 3; echo findProduct($N),"\n"; $N = 5; echo findProduct($N),"\n"; // This code is contributed by ajit ?>

Output:

12
34560

Time Complexity: O(N * logN), where O(log N) is the time complexity of modular multiplication.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, jit_t