📜  在不使用递归或欧几里得算法的情况下找到两个数字的HCF

📅  最后修改于: 2021-04-23 21:30:28             🧑  作者: Mango

给定两个整数xy ,任务是在不使用递归或欧几里得方法的情况下找到数字的HCF。

例子:

方法:两个数字的HCF是可以同时将两个数字相除的最大数字。如果两个数字中较小的一个可以将较大的数字相除,则HCF为较小的数字。否则从(较小/ 2)到1开始,检查当前元素是否将两个数相除。如果是,则它是必需的HCF。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the HCF of x and y
int getHCF(int x, int y)
{
 
    // Minimum of the two numbers
    int minimum = min(x, y);
 
    // If both the numbers are divisible
    // by the minimum of these two then
    // the HCF is equal to the minimum
    if (x % minimum == 0 && y % minimum == 0)
        return minimum;
 
    // Highest number between 2 and minimum/2
    // which can divide both the numbers
    // is the required HCF
    for (int i = minimum / 2; i >= 2; i--) {
 
        // If both the numbers
        // are divisible by i
        if (x % i == 0 && y % i == 0)
            return i;
    }
 
    // 1 divides every number
    return 1;
}
 
// Driver code
int main()
{
    int x = 16, y = 32;
    cout << getHCF(x, y);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return the HCF of x and y
static int getHCF(int x, int y)
{
 
    // Minimum of the two numbers
    int minimum = Math.min(x, y);
 
    // If both the numbers are divisible
    // by the minimum of these two then
    // the HCF is equal to the minimum
    if (x % minimum == 0 && y % minimum == 0)
        return minimum;
 
    // Highest number between 2 and minimum/2
    // which can divide both the numbers
    // is the required HCF
    for (int i = minimum / 2; i >= 2; i--)
    {
 
        // If both the numbers
        // are divisible by i
        if (x % i == 0 && y % i == 0)
            return i;
    }
 
    // 1 divides every number
    return 1;
}
 
// Driver code
public static void main(String[] args)
{
    int x = 16, y = 32;
    System.out.println(getHCF(x, y));
}
}
 
// This code is contributed by Code_Mech.


Python3
# Python3 implementation of the approach
 
# Function to return the HCF of x and y
def getHCF(x, y):
 
    # Minimum of the two numbers
    minimum = min(x, y)
 
    # If both the numbers are divisible
    # by the minimum of these two then
    # the HCF is equal to the minimum
    if (x % minimum == 0 and y % minimum == 0):
        return minimum
 
    # Highest number between 2 and minimum/2
    # which can divide both the numbers
    # is the required HCF
    for i in range(minimum // 2, 1, -1):
         
        # If both the numbers are divisible by i
        if (x % i == 0 and y % i == 0):
            return i
 
    # 1 divides every number
    return 1
 
# Driver code
x, y = 16, 32
print(getHCF(x, y))
 
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the HCF of x and y
static int getHCF(int x, int y)
{
 
    // Minimum of the two numbers
    int minimum = Math.Min(x, y);
 
    // If both the numbers are divisible
    // by the minimum of these two then
    // the HCF is equal to the minimum
    if (x % minimum == 0 && y % minimum == 0)
        return minimum;
 
    // Highest number between 2 and minimum/2
    // which can divide both the numbers
    // is the required HCF
    for (int i = minimum / 2; i >= 2; i--)
    {
 
        // If both the numbers
        // are divisible by i
        if (x % i == 0 && y % i == 0)
            return i;
    }
 
    // 1 divides every number
    return 1;
}
 
// Driver code
static void Main()
{
    int x = 16, y = 32;
    Console.WriteLine(getHCF(x, y));
}
}
 
// This code is contributed by mits


PHP
= 2; $i--)
    {
 
        // If both the numbers
        // are divisible by i
        if ($x % $i == 0 &&
            $y % $i == 0)
            return $i;
    }
 
    // 1 divides every number
    return 1;
}
 
// Driver code
$x = 16; $y = 32;
echo(getHCF($x, $y));
 
// This code is contributed
// by Code_Mech.
?>


Javascript


输出:
16