The Wayback Machine - https://web.archive.org/web/20240701125052/https://www.geeksforgeeks.org/gcd-factorials-two-numbers/
Open In App

GCD of factorials of two numbers

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers m and n. Find the GCD of  their factorial.
Examples : 

Input : n = 3, m = 4
Output : 6
Explanation:
Factorial of n = 1 * 2 * 3 = 6
Factorial of m = 1 * 2 * 3 * 4 = 24
GCD(6, 24) = 6.

Input : n = 9, m = 5
Output : 20
Explanation:
Factorial of n = 1 * 2 * 3 *4 * 5 * 6 * 7 * 8 * 9 = 362880
Factorial of m = 1 * 2 * 3 * 4 * 5 = 120
GCD(362880, 120) = 120

 

A simple solution is to find factorials of both numbers. Then find GCD of the computed factorials.
An efficient solution is based on the fact that GCD of two factorials is equal to smaller factorial (note that factorials have all terms common).
Below is the implementation of above approach. 
 

C++




// CPP program to find GCD of factorial of two
// numbers.
#include <bits/stdc++.h>
using namespace std;
 
int factorial(int x)
{
    if (x <= 1)
        return 1;
    int res = 2;
    for (int i = 3; i <= x; i++)
        res = res * i;
    return res;
}
 
int gcdOfFactorial(int m, int n)
{
    return factorial(min(m, n));
}
 
int main()
{
    int m = 5, n = 9;
    cout << gcdOfFactorial(m, n);
    return 0;
}


Java




// Java program to find GCD of factorial
// of two numbers.
public class FactorialGCD{
     
static int factorial(int x)
{
    if (x <= 1)
        return 1;
    int res = 2;
    for (int i = 3; i <= x; i++)
        res = res * i;
    return res;
}
 
static int gcdOfFactorial(int m, int n)
{
    int min = m < n ? m : n;
    return factorial(min);
}
 
    /* Driver program to test above functions */
    public static void main (String[] args)
    {
        int m = 5, n = 9;
         
        System.out.println(gcdOfFactorial(m, n));
    }
}
 
// This code is contributed by Prerna Saini


Python




# Python code to find GCD of factorials of
# two numbers.
import math
 
def gcdOfFactorial(m, n) :
    return math.factorial(min(m, n))
 
# Driver code
m = 5
n = 9
print(gcdOfFactorial(m, n))


C#




// C# program to find GCD of factorial
// of two numbers.
using System;
 
public class GFG{
      
    static int factorial(int x)
    {
        if (x <= 1)
            return 1;
             
        int res = 2;
         
        for (int i = 3; i <= x; i++)
            res = res * i;
             
        return res;
    }
      
    static int gcdOfFactorial(int m, int n)
    {
        int min = m < n ? m : n;
        return factorial(min);
    }
  
    /* Driver program to test above functions */
    public static void Main()
    {
         
        int m = 5, n = 9;
          
        Console.WriteLine(gcdOfFactorial(m, n));
    }
}
  
// This code is contributed by Anant Agarwal.


PHP




<?php
// PHP program to find GCD
// of factorial of two numbers.
 
function factorial($x)
{
    if ($x <= 1)
        return 1;
    $res = 2;
    for ($i = 3; $i <= $x; $i++)
        $res = $res * $i;
    return $res;
}
 
function gcdOfFactorial($m, $n)
{
    return factorial(min($m, $n));
}
 
// Driver Code
$m = 5; $n = 9;
echo gcdOfFactorial($m, $n);
 
// This code is contributed by ajit
?>


Javascript




<script>
 
// JavaScript program to find GCD of factorial
// of two numbers.
 
    function factorial(x) {
        if (x <= 1)
            return 1;
        var res = 2;
        for (i = 3; i <= x; i++)
            res = res * i;
        return res;
    }
 
    function gcdOfFactorial(m , n) {
        var min = m < n ? m : n;
        return factorial(min);
    }
 
    /* Driver program to test above functions */
     
        var m = 5, n = 9;
 
        document.write(gcdOfFactorial(m, n));
 
// This code is contributed by aashish1995
 
</script>


Output

120

Time complexity: O(min(m,n))
Auxiliary space: O(1)



Similar Reads

Numbers whose factorials end with n zeros
Given an integer n, we need to find the number of positive integers whose factorial ends with n zeros.Examples: Input : n = 1 Output : 5 6 7 8 9 Explanation: Here, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320 and 9! = 362880. Input : n = 2 Output : 10 11 12 13 14 Recommended PracticeN trailing zeroes in factorialsTry It! Prerequisite : Trailing zeros
6 min read
Count natural numbers whose factorials are divisible by x but not y
Given two numbers x and y (x &lt;= y), find out the total number of natural numbers, say i, for which i! is divisible by x but not y. Examples : Input : x = 2, y = 5 Output : 3 There are three numbers, 2, 3 and 4 whose factorials are divisible by x but not y. Input: x = 15, y = 25 Output: 5 5! = 120 % 15 = 0 &amp;&amp; 120 % 25 != 0 6! = 720 % 15 =
7 min read
Sum of factorials of Prime numbers in a Linked list
Given a Linked list of N integers, the task is to find the sum of factorials of each prime element in the list.Examples: Input: L1 = 4 -&gt; 6 -&gt; 2 -&gt; 12 -&gt; 3 Output: 8 Explanation: Prime numbers are 2 and 3, hence 2! + 3! = 2 + 6 = 8.Input: L1 = 7 -&gt; 4 -&gt; 5 Output: 5160 Explanation: Prime numbers are 7 and 5, hence 7! + 5! = 5160. A
15+ min read
Trailing number of 0s in product of two factorials
Given two integer N or M find the number of zero's trailing in product of factorials (N!*M!)? Examples: Input : N = 4, M = 5 Output : 1 Explanation : 4! = 24, 5! = 120 Product has only 1 trailing 0. Input : N = 127!, M = 57! Output : 44 As discussed in number of zeros in N! can be calculated by recursively dividing N by 5 and adding up the quotient
5 min read
Find last two digits of sum of N factorials
Given a number N, the task is to find the unit and tens places digit of the first N natural numbers factorials, i.e last two digit of 1!+2!+3!+….N! where N&lt;=10e18. Examples: Input: n = 2 Output: 3Explanation: 1! + 2! = 3, Last two digits are 3 Input: 4Output: 33Explanation: 1!+2!+3!+4!=33, Last two digits are 33 Naive Approach: In this approach,
7 min read
Queries for the product of first N factorials
Given Q[] queries where each query consists of an integer N, the task is to find the product of first N factorials for each of the query. Since the result could be large, compute it modulo 109 + 7.Examples: Input: Q[] = {4, 5} Output: 288 34560 Query 1: 1! * 2! * 3! * 4! = 1 * 2 * 6 * 24 = 288 Query 2: 1! * 2! * 3! * 4! * 5! = 1 * 2 * 6 * 24 * 120
7 min read
Print factorials of a range in right aligned format
Given two numbers m and n, the task is to find factorial of all numbers including m and n, and then print below format.Examples: Input : 6 10 Output : 720 5040 40320 362880 3628800 Input : 10 20 Output : 3628800 39916800 479001600 6227020800 87178291200 1307674368000 20922789888000 355687428096000 6402373705728000 121645100408832000 243290200817664
9 min read
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 MultiplicationApproach: The basic idea behind solving this problem is to just consider the problem of overflow during t
9 min read
Find the unit place digit of sum of N factorials
Given a number N, the task is to find units place digit of the first N natural numbers factorials, i.e. 1!+2!+3!+....N! where N&lt;=10e18.Examples: Input: n = 2 Output: 31! + 2! = 3Last digit is 3Input: n = 3Output: 91! + 2! + 3! = 9Last digit is 9 Brute Force Approach: In this approach, we are calculating the factorial of each number and then addi
10 min read
Find sum of factorials in an array
Given an array arr[] of N integers. The task is to find the sum of factorials of each element of the array. Examples: Input: arr[] = {7, 3, 5, 4, 8} Output: 45510 7! + 3! + 5! + 4! + 8! = 5040 + 6 + 120 + 24 + 40320 = 45510 Input: arr[] = {2, 1, 3} Output: 9 Approach: Implement a function factorial(n) that finds the factorial of n and initialize su
8 min read
Practice Tags :
three90RightbarBannerImg