使用C++找到模方程的解的数量

使用C++找到模方程的解的数量

在本文中,我们将解释什么是模方程的​​解,我们还将编写一个程序来查找模方程的多个解。这是基本示例 -

Input : X = 30 Y = 2 Output : 4, 7, 14, 28 Explanation : 30 mod 4 = 2 (equals Y), 30 mod 7 = 2 (equals Y), 30 mod 14 = 2 (equals Y), 30 mod 28 = 2 (equals Y) Input : X = 30 Y = 2 Output : 4, 7, 14, 28 Explanation : 30 mod 4 = 2 (equals Y), 30 mod 7 = 2 (equals Y), 30 mod 14 = 2 (equals Y), 30 mod 28 = 2 (equals Y)登录后复制

求解的方法

我们可以应用一种简单的方法,将 X 除以从 1 开始的每个整数,并检查它是否给出余数 Y,或者我们可以将 (X - Y) 除以每个整数,并且除以 (X - Y) 但不能除 X 的整数是解。让我们编写一个 C++ 程序来查找模方程的不同解。

示例

#include using namespace std; int numberofdivisor(int X, int Y){ int N = (X - Y); int noOfDivisors = 1; for (int i = 1; i Y) noOfDivisors++; } } return noOfDivisors; } void numberofsolutions(int X, int Y){ int noOfSolutions; if (X == Y) noOfSolutions = -1; if (X Y) noOfSolutions = numberofdivisor(X, Y); if (noOfSolutions == -1) { cout