hcf方法_Python GCD –查找GCD或HCF的4种方法

本文介绍了在Python中查找GCD(最高公因数,又称HCF)的四种方法:1) 使用循环,2) 通过递归,3) 利用内置的math.gcd()函数,4) 应用欧几里得算法。每种方法都有相应的程序示例和输出结果。

hcf方法

Here I will show you 4 different ways to find GCD in Python using program examples.

在这里,我将向您展示使用程序示例在Python中查找GCD的4种不同方法。

GCD also known as HCF (Highest Common Factor). So let’s see how we’ll do it.

GCD也称为HCF(最高公因数)。 因此,让我们看看我们将如何做。

Method 1: Using Loop

方法1:使用循环

n1 = 48
n2 = 36
 
#find smaller
if(n1>n2):
  smaller = n2
else:
  smaller = n1
  
#getting hcf  
i = 1
while(i<=smaller):
  if(n1%i==0 and n2%i ==0):
    hcf = i
  i  = i+1
 
print("hcf = ", hcf)

Output:

输出:

hcf = 12

hcf = 12

So in this program, first we assign values to n1 and n2, then we’ll find smaller number from both of the numbers.  After that we’ll start loop from 1 to smaller number to find a number which can be fully divisible with both of the numbers n1 and n2 and store into a new variable named as hcf. At the end of the loop we’ll get the highest number stored in hcf variable which can fully divide both of the numbers n1 and n2. That highest number will be our hcf.

因此,在此程序中,首先将值分配给n1和n2,然后从这两个数字中查找较小的数字。 之后,我们将从1到较小的数字开始循环,以找到一个可以被数字n1和n2完全整除的数字,并将其存储到名为hcf的新变量中 在循环结束时,我们将获得存储在hcf变量中的最高数字,该数字可以将数字n1和n2完全除。 最高的数字将是我们的hcf。

Method 2: Using Recursion

方法2:使用递归

def find_hcf(n1,n2):
    if(n2==0):
        return n1
    else:
        return find_hcf(n2,n1%n2)
 
n1 = 48
n2 = 36
 
hcf = find_hcf(n1,n2)
print ("highest common factor = ", hcf)

Output:

输出:

highest  common factor = 12

最高公因子= 12

So here we have a recursive function which receive two arguments and return the Highest common factor between them.

因此,这里有一个递归函数,该函数接收两个参数并返回它们之间的最高公因子。

Method 3: Using math.gcd()

方法3:使用math.gcd()

import math
 
n1 = 48
n2 = 36
 
hcf = math.gcd(n1,n2)
print("Highest Common Factor = ", hcf)

Output:

输出:

Highest Common Factor = 12

最高公因子= 12

Python has an inbuilt method to find out the GCD. We even doesn’t need to think how to code to find GCD. All we have to do is just use math.gcd() method and it will return the GCD.

Python有一个内置的方法来查找GCD。 我们甚至不需要思考如何编码才能找到GCD。 我们要做的只是使用math.gcd()方法,它将返回GCD。

Method 4: Using Euclidean Algorithm

方法4:使用欧几里得算法

Euclid’s algorithm, is an efficient method for computing the greatest common divisor (GCD) of two numbers. Here is the pseudocode to show how we can find GCD using Euclidean Algorithm.

Euclid算法是一种有效的方法,可以计算两个数字的最大公约数(GCD)。 这是伪代码,用于说明如何使用欧几里得算法找到GCD。

Pseudocode:

伪代码:

function gcd(a, b)

函数 gcd(a,b)

    while b ≠ 0

b≠0

       t := b;

t:= b;

       b := a mod b;

b:= mod b;

       a := t;

a:= t;

    return a;

返回

Program:

程序:

#implementing Euclidean algo 
def get_gcd	(x, y):
 
   while(y):
       x, y = y, x % y
 
   return x
 
n1 = 48
n2 = 36
 
hcf = get_gcd(n1,n2)
print("Highest Common Factor = ", hcf)

Output:

输出:

Highest Common Factor = 12

最高公因子= 12

In this program, get_gcd(int, int) function is used to implement the Euclidean algorithm. For more details on Euclidean algo please visit https://en.wikipedia.org/wiki/Euclidean_algorithm

在此程序中,使用get_gcd(int,int)函数来实现欧几里得算法。 有关欧几里得算法的更多详细信息,请访问https://en.wikipedia.org/wiki/Euclidean_algorithm

If you’ve any problem or suggestion related to python gcd programs then comment down below.

如果您有与python gcd程序相关的任何问题或建议,请在下方注释掉。

翻译自: https://www.thecrazyprogrammer.com/2018/05/python-gcd.html

hcf方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值