# Given two numbers a and b find all x such that a % x = b

Last Updated : 09 Nov, 2023

Given two numbers a and b find all x such that a % x = b.

Examples:

`Input : a = 21, b = 5Output : 2The answers of the Modular Equation are8 and 16 since 21 % 8 = 21 % 16 = 5.`

Here 3 cases arises :

1. If ( a < b ) then there will be no answer .
2. If ( a = b ) then all the numbers greater than a are the answer so there will be infinite solutions possible.
3. 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.

## C++

 `// C++ program to find x such that a % x is equal` `// to b.` `#include ` `using` `namespace` `std;`   `void` `modularEquation(``int` `a, ``int` `b)` `{` `    ``// if a is less than b then no solution` `    ``if` `(a < b) {` `        ``cout << ``"No solution possible "` `<< endl;` `        ``return``;` `    ``}`   `    ``// if a is equal to b then every number` `    ``// greater than a will be the solution` `    ``// so its infinity` `    ``if` `(a == b) {` `        ``cout << ``"Infinite Solution possible "` `<< endl;` `        ``return``;` `    ``}`   `    ``// all resultant number should be greater than` `    ``// b and (a-b) should be divisible by resultant` `    ``// number`   `    ``// count variable store the number of values` `    ``// possible` `    ``int` `count = 0;` `    ``int` `n = a - b;` `    ``int` `y = ``sqrt``(a - b);` `    ``for` `(``int` `i = 1; i <= y; ++i) {` `        ``if` `(n % i == 0) {`   `            ``// checking for both divisor and quotient` `            ``// whether they divide ( a-b ) completely` `            ``// and greater than b .` `            ``if` `(n / i > b)` `                ``count++;` `            ``if` `(i > b)` `                ``count++;` `        ``}` `    ``}`   `    ``// Here y is added twice in the last iteration` `    ``// so 1 y should be decremented to get correct` `    ``// solution` `    ``if` `(y * y == n && y > b)` `        ``count--;`   `    ``cout << count << endl;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `a = 21, b = 5;` `    ``modularEquation(a, b);` `    ``return` `0;` `}`

## Java

 `// Java program to find x such that ` `// a % x is equal to b.` `import` `java.io.*;` `class` `GFG {` `    `  `static` `void` `modularEquation(``int` `a, ``int` `b)` `{` `    ``// if a is less than b then no solution` `    ``if` `(a < b) {` `        ``System.out.println(``"No solution possible "``);` `        ``return``;` `    ``}`   `    ``// if a is equal to b then every number` `    ``// greater than a will be the solution` `    ``// so its infinity` `    ``if` `(a == b) {` `        ``System.out.println(``"Infinite Solution possible "``);` `        ``return``;` `    ``}`   `    ``// all resultant number should be greater` `    ``// than b and (a-b) should be divisible ` `    ``// by resultant number`   `    ``// count variable store the number of` `    ``// values possible` `    ``int` `count = ``0``;` `    ``int` `n = a - b;` `    ``int` `y = (``int``)Math.sqrt(a - b);` `    ``for` `(``int` `i = ``1``; i <= y; ++i) {` `        ``if` `(n % i == ``0``) {`   `            ``// checking for both divisor and` `            ``// quotient whether they divide` `            ``// ( a-b ) completely and ` `            ``// greater than b .` `            ``if` `(n / i > b)` `                ``count++;` `            ``if` `(i > b)` `                ``count++;` `        ``}` `    ``}`   `    ``// Here y is added twice in the last ` `    ``// iteration so 1 y should be decremented` `    ``// to get correct solution` `    ``if` `(y * y == n && y > b)` `        ``count--;`   `    ``System.out.println(count);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `a = ``21``, b = ``5``;` `    ``modularEquation(a, b);` `}` `}`   `// This code is contributed by Prerna Saini`

## Python3

 `# Python3 program to find x such ` `# that a % x is equal to b.`   `import` `math` `def` `modularEquation(a, b) :` `    `  `    ``# if a is less than b then no solution` `    ``if` `(a < b) :` `        ``print``(``"No solution possible "``) ` `        ``return` `    `  `    ``# if a is equal to b then every number` `    ``# greater than a will be the solution` `    ``# so its infinity` `    ``if` `(a ``=``=` `b) :` `        ``print``(``"Infinite Solution possible "``)` `        ``return` `    `  `    ``# all resultant number should be ` `    ``# greater than b and (a-b) should ` `    ``# be divisible by resultant number` ` `  `    ``# count variable store the number ` `    ``# of values possible` `    ``count ``=` `0` `    ``n ``=` `a ``-` `b` `    ``y ``=` `(``int``)(math.sqrt(a ``-` `b))` `    ``for` `i ``in` `range``(``1``, y``+``1``) :` `        ``if` `(n ``%` `i ``=``=` `0``) :` `            `  `            ``# checking for both divisor ` `            ``# and quotient whether they` `            ``# divide ( a-b ) completely` `            ``# and greater than b .` `            ``if` `(n ``/` `i > b) :` `                ``count ``=` `count ``+` `1` `            ``if` `(i > b) :` `                ``count ``=` `count  ``+` `1` `        `  `        `  ` `  `    ``# Here y is added twice in the ` `    ``# last iteration so 1 y should be` `    ``# decremented to get correct` `    ``# solution` `    ``if` `(y ``*` `y ``=``=` `n ``and` `y > b) :` `        ``count ``=` `count ``-` `1` ` `  `    ``print` `(count)` ` `  `# Driver code` `a ``=` `21` `b ``=` `5` `modularEquation(a, b)`   `# This code is contributed by Nikita Tiwari.`

## C#

 `// C# program to find x such that ` `// a % x is equal to b.` `using` `System;`   `class` `GFG {` `    `  `static` `void` `modularEquation(``int` `a, ``int` `b)` `{` `    ``// if a is less than b then no solution` `    ``if` `(a < b) {` `        ``Console.WriteLine(``"No solution possible "``);` `        ``return``;` `    ``}`   `    ``// if a is equal to b then every number` `    ``// greater than a will be the solution` `    ``// so its infinity` `    ``if` `(a == b) {` `        ``Console.WriteLine(``"Infinite Solution possible "``);` `        ``return``;` `    ``}`   `    ``// all resultant number should be greater` `    ``// than b and (a-b) should be divisible ` `    ``// by resultant number`   `    ``// count variable store the number of` `    ``// values possible` `    ``int` `count = 0;` `    ``int` `n = a - b;` `    ``int` `y = (``int``)Math.Sqrt(a - b);` `    ``for` `(``int` `i = 1; i <= y; ++i) {` `        ``if` `(n % i == 0) {`   `            ``// checking for both divisor and` `            ``// quotient whether they divide` `            ``// ( a-b ) completely and ` `            ``// greater than b .` `            ``if` `(n / i > b)` `                ``count++;` `            ``if` `(i > b)` `                ``count++;` `        ``}` `    ``}`   `    ``// Here y is added twice in the last ` `    ``// iteration so 1 y should be decremented` `    ``// to get correct solution` `    ``if` `(y * y == n && y > b)` `        ``count--;`   `    ``Console.WriteLine(count);` `}`   `// Driver code` `public` `static` `void` `Main()` `{` `    ``int` `a = 21, b = 5;` `    ``modularEquation(a, b);` `}` `}`   `//This code is contributed by vt_m.`

## Javascript

 ``

## PHP

 ` ``\$b``)` `                ``\$count``++;` `            ``if` `(``\$i` `> ``\$b``)` `                ``\$count``++;` `        ``}` `    ``}`   `    ``// Here y is added twice` `    ``// in the last iteration` `    ``// so 1 y should be ` `    ``// decremented to get correct` `    ``// solution` `    ``if` `(``\$y` `* ``\$y` `== ``\$n` `&& ``\$y` `> ``\$b``)` `        ``\$count``--;`   `    ``echo` `\$count` `;` `}`   `    ``// Driver Code` `    ``\$a` `= 21; ` `    ``\$b` `= 5;` `    ``modularEquation(``\$a``, ``\$b``);`   `// This code is contributed by anuj_67.` `?>`

Output:

`2`

Time Complexity: O(?(a-b))
Auxiliary Space: O(1)

Approach:

Steps for the approach:

• First, defined a function countModularEquation.
• which takes two integers a and b as input.
• returns the count of values of x that satisfy the equation a % x = b.
• Finally, the count of solutions is printed on the console.

Below is code implementation for the above approach:

## C++

 `#include ` `using` `namespace` `std;`   `int` `countModularEquation(``int` `a, ``int` `b)` `{` `    ``int` `count = 0;` `    ``for` `(``int` `x = 1; x <= a; x++) {` `        ``if` `(a % x == b) {` `            ``count++;` `        ``}` `    ``}` `    ``return` `count;` `}`   `int` `main()` `{` `    ``int` `a = 21, b = 5;` `    ``int` `count = countModularEquation(a, b);` `    ``cout << count << endl;` `    ``return` `0;` `}`

## Java

 `// Java program`   `import` `java.io.*;`   `class` `GFG {` `static` `int` `countModularEquation(``int` `a, ``int` `b)` `{` `    ``int` `count = ``0``;` `    ``for` `(``int` `x = ``1``; x <= a; x++) {` `        ``if` `(a % x == b) {` `            ``count++;` `        ``}` `    ``}` `    ``return` `count;` `}`   `public` `static` `void` `main(String[] args)` `{` `    ``int` `a = ``21``, b = ``5``;` `    ``int` `count = countModularEquation(a, b);` `    ``System.out.println(count);` `}` `}`   `// This code is contributed by Utkarsh Kumar`

## Python3

 `def` `countModularEquation(a, b):` `    ``count ``=` `0` `    ``for` `x ``in` `range``(``1``, a ``+` `1``):` `        ``if` `a ``%` `x ``=``=` `b:` `            ``count ``+``=` `1` `    ``return` `count`   `a ``=` `21` `b ``=` `5` `count ``=` `countModularEquation(a, b)` `print``(count)`

## C#

 `using` `System;`   `class` `GFG` `{` `    ``// Function to count the number of values of x from 1 to a` `    ``// for which (a % x) is equal to b` `    ``static` `int` `CountModularEquation(``int` `a, ``int` `b)` `    ``{` `        ``int` `count = 0;` `        ``for` `(``int` `x = 1; x <= a; x++)` `        ``{` `            ``// Check if (a % x) is equal to b` `            ``if` `(a % x == b)` `            ``{` `                ``count++;` `            ``}` `        ``}` `        ``return` `count;` `    ``}`   `    ``// Main method` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``// Input values` `        ``int` `a = 21, b = 5;`   `        ``// Call the function and print the result` `        ``int` `count = CountModularEquation(a, b);` `        ``Console.WriteLine(count);` `    ``}` `}`

## Javascript

 `// Function to count values of x such that a % x equals b` `function` `countModularEquation(a, b) {` `    ``let count = 0; ``// Initialize a count variable to keep track of the number of solutions` `    ``for` `(let x = 1; x <= a; x++) {` `        ``// Iterate through values of x from 1 to a` `        ``if` `(a % x === b) {` `            ``// Check if the condition a % x equals b is met` `            ``count++; ``// If true, increment the count` `        ``}` `    ``}` `    ``return` `count; ``// Return the final count of solutions` `}`   `// Main function` `function` `main() {` `    ``const a = 21; ``// Set the value of 'a'` `    ``const b = 5;  ``// Set the value of 'b'` `    ``const count = countModularEquation(a, b); ``// Call the function to count solutions` `    ``console.log(count); ``// Print the count to the console` `}`   `// Call the main function to start the program` `main();`

Output:

`2`

Time Complexity: O(a), where a is the value of the input parameter
Auxiliary Space: O(1)