📌  相关文章
📜  将两个给定数字除以它们的公因数

📅  最后修改于: 2021-05-31 20:26:56             🧑  作者: Mango

给定两个数字A和B,任务是将两个数字A和B除以它们的公因数。数字A和B小于10 ^ 8。
例子:

Input: A = 10, B = 15
Output: A = 2, B = 3
The common factors are 1, 5

Input: A = 100, B = 150
Output: A = 2, B = 3

天真的方法:从i = 1迭代到A和B的最小值,并检查i是否是A和B的因数。如果iAB的因数,则将两个数字A和B除以i。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// print the numbers after dividing
// them by their common factors
void divide(int a, int b)
{
    // iterate from 1 to minimum of a and b
    for (int i = 2; i <= min(a, b); i++) {
 
        // if i is the common factor
        // of both the numbers
        while (a % i == 0 && b % i == 0) {
            a = a / i;
            b = b / i;
        }
    }
 
    cout << "A = " << a << ", B = " << b << endl;
}
 
// Driver code
int main()
{
    int A = 10, B = 15;
 
    // divide A and B by their common factors
    divide(A, B);
 
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
 
class solution
{
 
// print the numbers after dividing
// them by their common factors
static void divide(int a, int b)
{
    // iterate from 1 to minimum of a and b
    for (int i = 2; i <= Math.min(a, b); i++) {
 
        // if i is the common factor
        // of both the numbers
        while (a % i == 0 && b % i == 0) {
            a = a / i;
            b = b / i;
        }
    }
 
    System.out.println("A = "+a+", B = "+b);
}
 
// Driver code
public static void main(String args[])
{
    int A = 10, B = 15;
 
    // divide A and B by their common factors
    divide(A, B);
 
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of above approach
 
# print the numbers after dividing
# them by their common factors
def divide(a, b) :
     
    # iterate from 1 to minimum of a and b
    for i in range(2, min(a, b) + 1) :
 
        # if i is the common factor
        # of both the numbers
        while (a % i == 0 and b % i == 0) :
            a = a // i
            b = b // i
 
    print("A =", a, ", B =", b)
 
# Driver code
if __name__ == "__main__" :
 
    A, B = 10, 15
 
    # divide A and B by their
    # common factors
    divide(A, B)
     
# This code is contributed by Ryuga


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
// print the numbers after dividing
// them by their common factors
static void divide(int a, int b)
{
    // iterate from 1 to minimum of a and b
    for (int i = 2; i <= Math.Min(a, b); i++)
    {
 
        // if i is the common factor
        // of both the numbers
        while (a % i == 0 && b % i == 0)
        {
            a = a / i;
            b = b / i;
        }
    }
    Console.WriteLine("A = "+a+", B = "+b);
}
 
// Driver code
static public void Main ()
{
    int A = 10, B = 15;
 
    // divide A and B by their common factors
    divide(A, B);
}
}
 
// This code is contributed by ajit.


PHP


Javascript


C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to calculate gcd of two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
void commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
 
    a = a / n;
    b = b / n;
 
    cout << "A = " << a << ", B = " << b << endl;
}
 
// Driver code
int main()
{
    int a = 10, b = 15;
    commDiv(a, b);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
     
// Function to calculate gcd
// of two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to calculate all common
// divisors of two given numbers
// a, b --> input integer numbers
static void commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
 
    a = a / n;
    b = b / n;
 
    System.out.println("A = " + a +
                       ", B = " + b);
}
 
// Driver code
public static void main(String[] args)
{
    int a = 10, b = 15;
    commDiv(a, b);
}
}
 
// This code is contributed
// by Code_Mech


Python3
# Python3 implementation of above approach
 
# Function to calculate gcd of two numbers
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)
 
# Function to calculate all common
# divisors of two given numbers
# a, b --> input eger numbers
def commDiv(a, b):
     
    # find gcd of a, b
    n = gcd(a, b)
 
    a = a // n
    b = b // n
 
    print("A =", a, ", B =", b)
 
# Driver code
a, b = 10, 15
commDiv(a, b)
 
# This code is contributed
# by mohit kumar


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to calculate gcd
// of two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to calculate all common
// divisors of two given numbers
// a, b --> input integer numbers
static void commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
 
    a = a / n;
    b = b / n;
 
    Console.WriteLine("A = " + a +
                    ", B = " + b);
}
 
// Driver code
public static void Main()
{
    int a = 10, b = 15;
    commDiv(a, b);
}
}
 
// This code is contributed
// by Code_Mech


PHP
 input integer numbers
function commDiv($a, $b)
{
    // find gcd of a, b
    $n = gcd($a, $b);
 
    $a = (int)($a / $n);
    $b = (int)($b / $n);
 
    echo "A = " . $a .
         ", B = " . $b . "\n";
}
 
// Driver code
$a = 10;
$b = 15;
commDiv($a, $b);
 
// This code is contributed by mits
?>


Javascript


输出:
A = 2, B = 3

一种有效的方法是使用两个数的公共除数中使用的相同概念。计算给定两个数字的最大公约数(gcd),然后将数字除以它们的gcd。

C++

// C++ implementation of above approach
#include 
using namespace std;
 
// Function to calculate gcd of two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
void commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
 
    a = a / n;
    b = b / n;
 
    cout << "A = " << a << ", B = " << b << endl;
}
 
// Driver code
int main()
{
    int a = 10, b = 15;
    commDiv(a, b);
 
    return 0;
}

Java

// Java implementation of above approach
class GFG
{
     
// Function to calculate gcd
// of two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to calculate all common
// divisors of two given numbers
// a, b --> input integer numbers
static void commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
 
    a = a / n;
    b = b / n;
 
    System.out.println("A = " + a +
                       ", B = " + b);
}
 
// Driver code
public static void main(String[] args)
{
    int a = 10, b = 15;
    commDiv(a, b);
}
}
 
// This code is contributed
// by Code_Mech

Python3

# Python3 implementation of above approach
 
# Function to calculate gcd of two numbers
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)
 
# Function to calculate all common
# divisors of two given numbers
# a, b --> input eger numbers
def commDiv(a, b):
     
    # find gcd of a, b
    n = gcd(a, b)
 
    a = a // n
    b = b // n
 
    print("A =", a, ", B =", b)
 
# Driver code
a, b = 10, 15
commDiv(a, b)
 
# This code is contributed
# by mohit kumar

C#

// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to calculate gcd
// of two numbers
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to calculate all common
// divisors of two given numbers
// a, b --> input integer numbers
static void commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
 
    a = a / n;
    b = b / n;
 
    Console.WriteLine("A = " + a +
                    ", B = " + b);
}
 
// Driver code
public static void Main()
{
    int a = 10, b = 15;
    commDiv(a, b);
}
}
 
// This code is contributed
// by Code_Mech

的PHP

 input integer numbers
function commDiv($a, $b)
{
    // find gcd of a, b
    $n = gcd($a, $b);
 
    $a = (int)($a / $n);
    $b = (int)($b / $n);
 
    echo "A = " . $a .
         ", B = " . $b . "\n";
}
 
// Driver code
$a = 10;
$b = 15;
commDiv($a, $b);
 
// This code is contributed by mits
?>

Java脚本


输出:
A = 2, B = 3

时间复杂度: O(n)