Given two numbers a and b find all x such that a % x = b .
Input : a = 21, b = 5 Output : 2 The answers of the Modular Equation are 8 and 16 since 21 % 8 = 21 % 16 = 5 .
Here 3 cases arises :
- If ( a < b ) then there will be no answer .
- If ( a = b ) then all the numbers greater than a are the answer so there will be infinite solutions possible.
- If ( a > b ) Suppose x is an answer to our equation. Then x divides (a – b). Also since a % x = b then b < x. These conditions are necessary and sufficient as well. So the answer is number of divisors of a – b which are strictly greater than b which can be solved in O(sqrt( a-b )). Here only one case arises which we have to deal separately when (a-b) is perfect square then we will add its square root two times so we have to subtract one times, if this case arises.
- Find two numbers whose sum and GCD are given
- Find LCM of rational numbers
- Find k numbers which are powers of 2 and have sum N | Set 1
- Program to find LCM of 2 numbers without using GCD
- Find two numbers with sum and product both same as N
- Find the sum of first N odd Fibonacci numbers
- Find XOR of numbers from the range [L, R]
- Find max of two Rational numbers
- To find sum of two numbers without using any operator
- Find the sum of the all amicable numbers up to N
- Find two prime numbers with given sum
- Program to find LCM of two numbers
- Program to find GCD or HCF of two numbers
- Find sum of N-th group of Natural Numbers
- Find the sum of numbers from 1 to n excluding those which are powers of K
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.