给定两个整数X和Y ,任务是查找并打印将X和Y相除以产生相同余数的数字。
例子:
Input: X = 1, Y = 5
Output: 1, 2, 4
Explanation:
Let the number be M. It can be any value in the range [1, 5]:
If M = 1, 1 % 1 = 0 and 5 % 1 = 0
If M = 2, 1 % 2 = 1 and 5 % 2 = 1
If M = 3, 1 % 3 = 1 and 5 % 3 = 2
If M = 4, 1 % 4 = 1 and 5 % 4 = 1
If M = 5, 1 % 5 = 1 and 5 % 5 = 0
Therefore, the possible M values are 1, 2, 4
Input: X = 8, Y = 10
Output: 1, 2
幼稚的方法:针对此问题的幼稚的方法是检查范围[1,max(X,Y)]内M的所有可能值的模值,并在条件满足时打印M的值。
下面是上述方法的实现:
C++
// C++ program to find numbers
// that divide X and Y
// to produce the same remainder
#include
using namespace std;
// Function to find
// the required number as M
void printModulus(int X, int Y)
{
// Finding the maximum
// value among X and Y
int n = max(X, Y);
// Loop to iterate through
// maximum value among X and Y.
for (int i = 1; i <= n; i++) {
// If the condition satisfies, then
// print the value of M
if (X % i == Y % i)
cout << i << " ";
}
}
// Driver code
int main()
{
int X, Y;
X = 10;
Y = 20;
printModulus(X, Y);
return 0;
}
Java
// Java program to find numbers
// that divide X and Y
// to produce the same remainder
class GFG{
// Function to find
// the required number as M
static void printModulus(int X, int Y)
{
// Finding the maximum
// value among X and Y
int n = Math.max(X, Y);
// Loop to iterate through
// maximum value among X and Y.
for (int i = 1; i <= n; i++) {
// If the condition satisfies, then
// print the value of M
if (X % i == Y % i)
System.out.print(i + " ");
}
}
// Driver code
public static void main(String[] args)
{
int X, Y;
X = 10;
Y = 20;
printModulus(X, Y);
}
}
// This code is contributed by Princi Singh
Python3
# Python program to find numbers
# that divide X and Y
# to produce the same remainder
# Function to find
# the required number as M
def printModulus( X, Y):
# Finding the maximum
# value among X and Y
n = max(X, Y)
# Loop to iterate through
# maximum value among X and Y.
for i in range(1, n + 1):
# If the condition satisfies, then
# print the value of M
if (X % i == Y % i):
print(i,end=" ")
# Driver code
X = 10
Y = 20
printModulus(X, Y)
# This code is contributed by Atul_kumar_Shrivastava
C#
// C# program to find numbers
// that divide X and Y
// to produce the same remainder
using System;
class GFG{
// Function to find
// the required number as M
static void printModulus(int X, int Y)
{
// Finding the maximum
// value among X and Y
int n = Math.Max(X, Y);
// Loop to iterate through
// maximum value among X and Y.
for (int i = 1; i <= n; i++) {
// If the condition satisfies, then
// print the value of M
if (X % i == Y % i)
Console.Write(i + " ");
}
}
// Driver code
public static void Main()
{
int X, Y;
X = 10;
Y = 20;
printModulus(X, Y);
}
}
// This code is contributed by AbhiThakur
Javascript
CPP
// C++ program to find numbers
// that divide X and Y to
// produce the same remainder
#include
using namespace std;
// Function to print all the possible values
// of M such that X % M = Y % M
void printModulus(int X, int Y)
{
// Finding the absolute difference
// of X and Y
int d = abs(X - Y);
// Iterating from 1
int i = 1;
// Loop to print all the factors of D
while (i * i <= d) {
// If i is a factor of d, then print i
if (d % i == 0) {
cout << i << " ";
// If d / i is a factor of d,
// then print d / i
if (d / i != i)
cout << d / i << " ";
}
i++;
}
}
// Driver code
int main()
{
int X = 10;
int Y = 26;
printModulus(X, Y);
return 0;
}
Java
// Java program to find numbers
// that divide X and Y to
// produce the same remainder
import java.util.*;
class GFG{
// Function to print all the possible values
// of M such that X % M = Y % M
static void printModulus(int X, int Y)
{
// Finding the absolute difference
// of X and Y
int d = Math.abs(X - Y);
// Iterating from 1
int i = 1;
// Loop to print all the factors of D
while (i * i <= d) {
// If i is a factor of d, then print i
if (d % i == 0) {
System.out.print(i+ " ");
// If d / i is a factor of d,
// then print d / i
if (d / i != i)
System.out.print(d / i+ " ");
}
i++;
}
}
// Driver code
public static void main(String[] args)
{
int X = 10;
int Y = 26;
printModulus(X, Y);
}
}
// This code is contributed by Princi Singh
Python3
# Python program to find numbers
# that divide X and Y to
# produce the same remainder
# Function to prall the possible values
# of M such that X % M = Y % M
def printModulus(X, Y):
# Finding the absolute difference
# of X and Y
d = abs(X - Y);
# Iterating from 1
i = 1;
# Loop to prall the factors of D
while (i * i <= d):
# If i is a factor of d, then pri
if (d % i == 0):
print(i, end="");
# If d / i is a factor of d,
# then prd / i
if (d // i != i):
print(d // i, end=" ");
i+=1;
# Driver code
if __name__ == '__main__':
X = 10;
Y = 26;
printModulus(X, Y);
# This code contributed by Princi Singh
C#
// C# program to find numbers
// that divide X and Y to
// produce the same remainder
using System;
public class GFG{
// Function to print all the possible values
// of M such that X % M = Y % M
static void printModulus(int X, int Y)
{
// Finding the absolute difference
// of X and Y
int d = Math.Abs(X - Y);
// Iterating from 1
int i = 1;
// Loop to print all the factors of D
while (i * i <= d) {
// If i is a factor of d, then print i
if (d % i == 0) {
Console.Write(i+ " ");
// If d / i is a factor of d,
// then print d / i
if (d / i != i)
Console.Write(d / i+ " ");
}
i++;
}
}
// Driver code
public static void Main(String[] args)
{
int X = 10;
int Y = 26;
printModulus(X, Y);
}
}
// This code contributed by Princi Singh
Javascript
输出:
1 2 5 10
时间复杂度: O(max(X,Y))
高效方法:假设Y比X大D的差。
- 那么Y可以表示为
Y = X + D
and
Y % M = (X + D) % M
= (X % M) + (D % M)
- 现在,条件变为X%M和X%M + D%M是否相等。
- 在此,由于X%M在两侧都是公共的,所以如果对于某些M, D%M = 0 ,则M的值为真。
- 因此,M的要求值将成为D的因数。
下面是上述方法的实现:
CPP
// C++ program to find numbers
// that divide X and Y to
// produce the same remainder
#include
using namespace std;
// Function to print all the possible values
// of M such that X % M = Y % M
void printModulus(int X, int Y)
{
// Finding the absolute difference
// of X and Y
int d = abs(X - Y);
// Iterating from 1
int i = 1;
// Loop to print all the factors of D
while (i * i <= d) {
// If i is a factor of d, then print i
if (d % i == 0) {
cout << i << " ";
// If d / i is a factor of d,
// then print d / i
if (d / i != i)
cout << d / i << " ";
}
i++;
}
}
// Driver code
int main()
{
int X = 10;
int Y = 26;
printModulus(X, Y);
return 0;
}
Java
// Java program to find numbers
// that divide X and Y to
// produce the same remainder
import java.util.*;
class GFG{
// Function to print all the possible values
// of M such that X % M = Y % M
static void printModulus(int X, int Y)
{
// Finding the absolute difference
// of X and Y
int d = Math.abs(X - Y);
// Iterating from 1
int i = 1;
// Loop to print all the factors of D
while (i * i <= d) {
// If i is a factor of d, then print i
if (d % i == 0) {
System.out.print(i+ " ");
// If d / i is a factor of d,
// then print d / i
if (d / i != i)
System.out.print(d / i+ " ");
}
i++;
}
}
// Driver code
public static void main(String[] args)
{
int X = 10;
int Y = 26;
printModulus(X, Y);
}
}
// This code is contributed by Princi Singh
Python3
# Python program to find numbers
# that divide X and Y to
# produce the same remainder
# Function to prall the possible values
# of M such that X % M = Y % M
def printModulus(X, Y):
# Finding the absolute difference
# of X and Y
d = abs(X - Y);
# Iterating from 1
i = 1;
# Loop to prall the factors of D
while (i * i <= d):
# If i is a factor of d, then pri
if (d % i == 0):
print(i, end="");
# If d / i is a factor of d,
# then prd / i
if (d // i != i):
print(d // i, end=" ");
i+=1;
# Driver code
if __name__ == '__main__':
X = 10;
Y = 26;
printModulus(X, Y);
# This code contributed by Princi Singh
C#
// C# program to find numbers
// that divide X and Y to
// produce the same remainder
using System;
public class GFG{
// Function to print all the possible values
// of M such that X % M = Y % M
static void printModulus(int X, int Y)
{
// Finding the absolute difference
// of X and Y
int d = Math.Abs(X - Y);
// Iterating from 1
int i = 1;
// Loop to print all the factors of D
while (i * i <= d) {
// If i is a factor of d, then print i
if (d % i == 0) {
Console.Write(i+ " ");
// If d / i is a factor of d,
// then print d / i
if (d / i != i)
Console.Write(d / i+ " ");
}
i++;
}
}
// Driver code
public static void Main(String[] args)
{
int X = 10;
int Y = 26;
printModulus(X, Y);
}
}
// This code contributed by Princi Singh
Java脚本
输出:
1 16 2 8 4
时间复杂度分析O(sqrt(D)) ,其中D是值X和Y之间的差。