# GCD of factorials of two numbers

Given two numbers m and n. Find the GCD of the 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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)) ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

120

## Recommended Posts:

- Numbers whose factorials end with n zeros
- Count natural numbers whose factorials are divisible by x but not y
- Product of first N factorials
- Find last two digits of sum of N factorials
- Queries for the product of first N factorials
- Find sum of factorials in an array
- Trailing number of 0s in product of two factorials
- Calculating Factorials using Stirling Approximation
- Find the unit place digit of sum of N factorials
- Print factorials of a range in right aligned format
- Check if a given number divides the sum of the factorials of its digits
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10

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.