📌  相关文章
📜  查找将X和Y除以产生相同余数的数字

📅  最后修改于: 2021-05-07 01:37:22             🧑  作者: Mango

给定两个整数XY ,任务是查找并打印将X和Y相除以产生相同余数的数字。
例子:

幼稚的方法:针对此问题的幼稚的方法是检查范围[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))
高效方法:假设YXD的差。

  • 那么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之间的差。