![]() |
VOOZH | about |
Given two integers A and B, the task is to find how many integer values of X satisfy the modular equation: A mod X = B. Here, X is called a solution of the modular equation. In simple terms, find all integers X such that when A is divided by X, the remainder is B.
For Example:
Input: A = 26, B = 2
Output: 6
Possible X values -> {3, 4, 6, 8, 12, 24}
From the definition of modulus:
A = X * Y + B --> A - B = X * Y
So, X must be a divisor of (A − B). Also, because remainder B must be strictly less than the divisor X, only those divisors of (A − B) that are greater than B are valid solutions.
Let’s explore different methods to do this in Python.
This is the fastest method to count all divisors of (A - B) that are greater than B using a single loop up to √(A - B).
For A = 26 and B = 2 , X can take 6 values
Explanation:
This version uses the math module for clearer square root calculation and keeps the logic simple.
For A = 21 and B = 5 , X can take 2 values
Explanation:
This method checks every possible X from B + 1 to A and counts those that satisfy the modular equation. It’s simple but not efficient for large numbers.
For A = 26 and B = 2 , X can take 6 values
Explanation: