Product of first N factorials
Last Updated :
01 Mar, 2023
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 the 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 the product of two large numbers under modulo, we use the same approach as exponentiation under modulo… In the multiplication function, we use + instead of *.
Steps to solve the problem:
- Define a function named mulmod() that takes three arguments a, b, and mod.
- Inside mulmod(), initialize res to 0.
- Compute a = a % mod.
- While b > 0, perform the following operations:
- If b % 2 == 1, add a to res modulo mod.
- Multiply a by 2 and compute a = a % mod.
- Divide b by 2 and discard the remainder. Return res % mod.
- Define a function named findProduct() that takes a single argument N.
- Inside findProduct(), initialize product and fact to 1.
- Set MOD = 1e9 + 7.
- For i in the range 1 to N, perform the following operations:
- Compute fact as the product of fact and i modulo MOD, i.e., fact = mulmod(fact, i, MOD)
- Compute product as the product of product and fact modulo MOD, i.e., product = mulmod(product, fact, MOD).
- If product is zero, return 0.
- Return product
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
long long int mulmod( long long int a, long long int b,
long long int mod)
{
long long int res = 0;
a = a % mod;
while (b > 0) {
if (b % 2 == 1)
res = (res + a) % mod;
a = (a * 2) % mod;
b /= 2;
}
return res % mod;
}
long long int findProduct( long long int N)
{
long long int product = 1, fact = 1;
long long int MOD = 1e9 + 7;
for ( int i = 1; i <= N; i++) {
fact = mulmod(fact, i, MOD);
product = mulmod(product, fact, MOD);
if (product == 0)
return 0;
}
return product;
}
int main()
{
long long int N = 3;
cout << findProduct(N) << endl;
N = 5;
cout << findProduct(N) << endl;
return 0;
}
|
Java
class GFG{
static double mulmod( long a, long b,
long mod)
{
long res = 0 ;
a = a % mod;
while (b > 0 ) {
if (b % 2 == 1 )
res = (res + a) % mod;
a = (a * 2 ) % mod;
b /= 2 ;
}
return res % mod;
}
static long findProduct( long N)
{
long product = 1 , fact = 1 ;
long MOD = ( long )(1e9 + 7 );
for ( int i = 1 ; i <= N; i++) {
fact = ( long )mulmod(fact, i, MOD);
product = ( long )mulmod(product, fact, MOD);
if (product == 0 )
return 0 ;
}
return product;
}
public static void main(String[] args)
{
long N = 3 ;
System.out.println(findProduct(N));
N = 5 ;
System.out.println(findProduct(N));
}
}
|
Python3
def mulmod(a, b, mod):
res = 0
a = a % mod
while (b > 0 ):
if (b % 2 = = 1 ):
res = (res + a) % mod
a = (a * 2 ) % mod
b / / = 2
return res % mod
def findProduct(N):
product = 1 ; fact = 1
MOD = 1e9 + 7
for i in range ( 1 , N + 1 ):
fact = mulmod(fact, i, MOD)
product = mulmod(product, fact, MOD)
if not product:
return 0
return int (product)
N = 3
print (findProduct(N))
N = 5
print (findProduct(N))
|
C#
using System;
public class GFG{
static double mulmod( long a, long b,
long mod)
{
long res = 0;
a = a % mod;
while (b > 0) {
if (b % 2 == 1)
res = (res + a) % mod;
a = (a * 2) % mod;
b /= 2;
}
return res % mod;
}
static long findProduct( long N)
{
long product = 1, fact = 1;
long MOD = ( long )(1e9 + 7);
for ( int i = 1; i <= N; i++) {
fact = ( long )mulmod(fact, i, MOD);
product = ( long )mulmod(product, fact, MOD);
if (product == 0)
return 0;
}
return product;
}
static public void Main (){
long N = 3;
Console.WriteLine(findProduct(N));
N = 5;
Console.WriteLine(findProduct(N));
}
}
|
PHP
<?php
function mulmod( $a , $b , $mod )
{
$res = 0;
$a = $a % $mod ;
while ( $b > 0)
{
if ( $b % 2 == 1)
$res = ( $res + $a ) % $mod ;
$a = ( $a * 2) % $mod ;
$b /= 2;
}
return $res % $mod ;
}
function findProduct( $N )
{
$product = 1;
$fact = 1;
$MOD = 1000000000;
for ( $i = 1; $i <= $N ; $i ++)
{
$fact = mulmod( $fact , $i , $MOD );
$product = mulmod( $product , $fact , $MOD );
if ( $product == 0)
return 0;
}
return $product ;
}
$N = 3;
echo findProduct( $N ), "\n" ;
$N = 5;
echo findProduct( $N ), "\n" ;
?>
|
Javascript
<script>
function mulmod(a, b, mod)
{
let res = 0;
a = a % mod;
while (b > 0) {
if (b % 2 == 1)
res = (res + a) % mod;
a = (a * 2) % mod;
b = parseInt(b / 2, 10);
}
return res % mod;
}
function findProduct(N)
{
let product = 1, fact = 1;
let MOD = (1e9 + 7);
for (let i = 1; i <= N; i++) {
fact = mulmod(fact, i, MOD);
product = mulmod(product, fact, MOD);
if (product == 0)
return 0;
}
return product;
}
let N = 3;
document.write(findProduct(N) + "</br>" );
N = 5;
document.write(findProduct(N));
</script>
|
Time Complexity: O(N * logN), where O(log N) is the time complexity of modular multiplication.
Auxiliary Space: O(1) because it is using constant space for variables
Share your thoughts in the comments
Please Login to comment...