C/C++ Program for Number of solutions to the Modular Equations?

CC++Server Side ProgrammingProgramming

In mathematics, a modular equation is an algebraic equation satisfied by moduli, in the sense of moduli problem. That is, given a number of functions on a moduli space, a modular equation is an equation holding between them, or in other words an identity for moduli.

The most frequent use of the term modular equation is in relation to the moduli problems for elliptic curves. In that case, the moduli space itself is of dimension one. That implies that any two rational functions F and G, in the function field of the modular curve, will satisfy a modular equation P(F, G) = 0 with P a non-zero polynomial of two variables over the complex numbers. For the suitable non-degenerate choice of F and G, the equation P(X,Y) = 0 will actually define the modular curve.

You just saw a weird kind of mathematical expression of the form

B ≡ (A mod X)

This says that B is congruent to A modulo X. Let's take an example,

21 ≡ 5( mod 4)

A symbol equiv means “Equivalence”. In the above equation, 21 and 5 are equivalent. This is because 21 modulo 4 = 1 is equal to 5 modulo 4 = 1. Another example is 51 ≡ 16( mod 7)

In this problem, we have two integers a and b and we have to find the number of possible values x that follow the modular equation (A mod X)=B where X solution of the modular equation.

For Example

Input: A = 26, B = 2
Output: X can take 6 values


X can be equal to any of {3, 4, 6, 8, 12, 24} as A modulus any of these values equals 2 i. e., (26 mod 3) = (26 mod 4) = (26 mod 6) = (26 mod 8) = .... = 2

We have the equation A mod X = B


if (A = B) then there will be infinite many values where A always be greater than X.

if (A < B) then there is not possible that X can hold the modular equation.

Now only last case is left that (A > B).

Now, in this case, we will use relation

Dividend = Divisor * Quotient + Remainder

X i.e. Divisors given A i.e Dividend and B i.e., remainder.


A = X * Quotient + B

Let Quotient be represented as Y

∴ A = X * Y + B

A - B = X * Y

∴ To get integral values of Y,

we need to take all X such that X divides (A - B)

∴ X is a divisor of (A - B)

finding the divisors of (A – B) is the main problem and the number of such divisors is the possible values X can take.

we know that A mod X solution values will be from (0 to X – 1) take all such X such that X > B.

In this way we can conclude by saying that the number of divisors of (A – B) greater than B, with all possible values X which can de satisfy A mod X = B


#include <iostream>
#include <math.h>
using namespace std;
int Divisors(int A, int B) {
   int N = (A - B);
   int D = 0;
   for (int i = 1; i <= sqrt(N); i++) {
      if ((N % i) == 0) {
         if (i > B)
         if ((N / i) != i && (N / i) > B)
   return D;
int PossibleWaysUtil(int A, int B) {
   if (A == B)
      return -1;
   if (A < B)
      return 0;
   int D = 0;
   D = Divisors(A, B);
   return D;
int main() {
   int A = 26, B = 2;
   int Sol = PossibleWaysUtil(A, B);
   if (Sol == -1) {
      cout <<" X can take Infinitely many values greater than " << A << "\n";
   } else {
      cout << " X can take " << Sol << " values\n";
      return 0;
Updated on 19-Aug-2019 07:24:21