📜  n重复x和y次形成的两个数字的GCD

📅  最后修改于: 2021-05-07 07:14:47             🧑  作者: Mango

给定三个正整数nxy 。任务是打印n次重复x次形成的数字和n次重复y次形成的数字的最大公约数。
0 <= n,x,y <= 1000000000。
例子 :

Input : n = 123, x = 2, y = 3.
Output : 123
Number formed are 123123 and 123123123.
Greatest Common Divisor of 123123 and
123123123 is 123.

Input : n = 4, x = 4, y = 6.
Output : 44

这个想法是基于欧几里德算法来计算两个数的GCD。
令f(n,x)是一个将n重复x次的函数。因此,我们需要找到GCD(f(n,x),f(n,y))。
令n = 123,x = 3,y = 2。
因此,第一个数字A为f(123,3)= 123123123,第二个数字B为f(123,2)=123123。我们知道,使用此属性,GCD(A,B)= GCD(A – B,B)只要B’小于A,我们就可以从第一个A中减去B的任何倍数,例如B’。
因此,A = 123123123,而B’可以为123123000。减去A将变为123,而B保持不变。
因此,A = A – B’= f(n,x – y)。
因此,GCD(f(n,x),f(n,y))= GCD(f(n,x – y),f(n,y))
我们可以得出以下结论:

GCD(f(n, x), f(n, y)) = f(n, GCD(x, y)). 

以下是基于此方法的实现:

CPP
// C++ program to print Greatest Common Divisor
// of number formed by N repeating x times and
// y times.
#include
using namespace std;
 
// Return the Greatest common Divisor of two numbers.
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b%a, a);
}
 
// Prints Greatest Common Divisor of number formed
// by n repeating x times and y times.
void findgcd(int n, int x, int y)
{
    // Finding GCD of x and y.
    int g = gcd(x,y);
 
    // Print n, g times.
    for (int i = 0; i < g; i++)
        cout << n;
}
 
// Driven Program
int main()
{
    int n = 123, x = 5, y = 2;
    findgcd(n, x, y);
    return 0;
}


Java
// Java program to print Greatest Common Divisor
// of number formed by N repeating x times and
// y times
class GFG {
     
    // Return the Greatest common Divisor
    // of two numbers.
    static int gcd(int a, int b) {
         
        if (a == 0)
            return b;
             
        return gcd(b % a, a);
    }
     
    // Prints Greatest Common Divisor of
    // number formed by n repeating x
    // times and y times.
    static void findgcd(int n, int x, int y) {
         
        // Finding GCD of x and y.
        int g = gcd(x, y);
     
        // Print n, g times.
        for (int i = 0; i < g; i++)
            System.out.print(n);
    }
     
    // Driver code
    public static void main(String[] args) {
         
        int n = 123, x = 5, y = 2;
        findgcd(n, x, y);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to print Greatest
# Common Divisor of number formed
# by N repeating x times and y times
 
# Return the Greatest common Divisor
# of two numbers.
def gcd(a, b):
     
    if (a == 0):
        return b
     
    return gcd(b % a, a)
 
# Prints Greatest Common Divisor of
# number formed by n repeating x times
# and y times.
def findgcd(n, x, y):
 
    # Finding GCD of x and y.
    g = gcd(x, y)
 
    # Print n, g times.
    for i in range(g):
        print(n)
 
# Driver code
n = 123
x = 5
y = 2
 
findgcd(n, x, y)
 
# This code is contributed by Anant Agarwal.


C#
// C# program to print Greatest Common
// Divisor of number formed by N
// repeating x times and y times
using System;
 
class GFG {
     
    // Return the Greatest common
    // Divisor of two numbers.
    static int gcd(int a, int b)
    {
         
        if (a == 0)
            return b;
             
        return gcd(b % a, a);
    }
     
    // Prints Greatest Common
    // Divisor of number formed
    // by n repeating x times
    // and y times.
    static void findgcd(int n,
                      int x, int y)
    {
         
        // Finding GCD of x and y.
        int g = gcd(x, y);
     
        // Print n, g times.
        for (int i = 0; i < g; i++)
            Console.Write(n);
    }
     
    // Driver code
    public static void Main() {
         
        int n = 123, x = 5, y = 2;
         
        findgcd(n, x, y);
    }
}
 
// This code is contributed by
// nitin mittal.


PHP


Javascript


输出 :

123