Given two numbers x and y (x <= y), find out the total number of natural numbers, say i, for which i! is divisible by x but not y.
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 && 120 % 25 != 0 6! = 720 % 15 = 0 && 720 % 25 != 0 7! = 5040 % 15 = 0 && 5040 % 25 != 0 8! = 40320 % 15 = 0 && 40320 % 25 != 0 9! = 362880 % 15 = 0 && 362880 % 25 != 0 So total count = 5 Input: x = 10, y = 15 Output: 0
For all numbers greater than or equal to y, their factorials are divisible by y. So all natural numbers to be counted must be less than y.
A simple solution is to iterate from 1 to y-1 and for every number i check if i! is divisible by x and not divisible by y. If we apply this naive approach, we wouldn’t go above 20! or 21! (long long int will have its upper limit)
A better solution is based on below post.
Find the first natural number whose factorial is divisible by x
We find the first natural numbers whose factorials are divisible by x! and y! using above approach. Let the first natural numbers whose factorials are divisible by x and y be xf and yf respectively. Our final answer would be yf – xf. This formula is based on the fact that if i! is divisible by a number x, then (i+1)!, (i+2)!, … are also divisible by x.
Below is the implementation.
This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Count numbers in range 1 to N which are divisible by X but not by Y
- Number of substrings divisible by 8 but not by 3
- Numbers whose factorials end with n zeros
- Minimum number of Factorials whose sum is equal to N
- Find the first natural number whose factorial is divisible by x
- GCD of factorials of two numbers
- Sum of factorials of Prime numbers in a Linked list
- Find the Nth natural number which is not divisible by A
- Sum of first N natural numbers which are divisible by 2 and 7
- Check whether factorial of N is divisible by sum of first N natural numbers
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Check if the concatenation of first N natural numbers is divisible by 3
- Length of longest subarray whose sum is not divisible by integer K
- Calculating Factorials using Stirling Approximation
- Trailing number of 0s in product of two factorials
- Queries for the product of first N factorials
- Print factorials of a range in right aligned format
- Product of first N factorials
- Find the unit place digit of sum of N factorials
- Find last two digits of sum of N factorials