Given two numbers m and n. Find the GCD of the their factorial.
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
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Numbers whose factorials end with n zeros
- Count natural numbers whose factorials are divisible by x but not y
- Sum of factorials of Prime numbers in a Linked list
- 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
- Minimum number of Factorials whose sum is equal to N
- Calculating Factorials using Stirling Approximation
- Check if a given number divides the sum of the factorials of its digits
- Find the unit place digit of sum of N factorials
- Print factorials of a range in right aligned format
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of numbers upto M divisible by given Prime Numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Check if a given pair of Numbers are Betrothed numbers or not
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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 : jit_t