如何使用 Python 查找 HCF 或 GCD?
在本文中,我们将向您展示如何在 Python 中查找HCF(最大公因数)或GCD(最大公约数)。以下是完成此任务的各种方法
使用 For 循环
使用欧几里得算法
使用 While 循环
使用递归(朴素方法)
处理 HCF 中的负数
什么是 H.C.F. 或 G.C.D?
完美地整除两个给定数字的最大正整数称为最大公因数(H.C.F.)或最大公约数(G.C.D.)。
示例 - 12 和 14 的 HCF 为2。
使用 For 循环
算法(步骤)
以下是执行所需任务应遵循的算法/步骤 –
创建一个变量来存储输入数字 1。
创建另一个变量来存储输入数字 2。
使用if 条件语句检查第一个数字是否大于第二个数字。
如果条件为真,则将第二个数字视为较小数字。
否则将第一个数字视为较小数字。
使用for 循环使用range()函数遍历从 1 到小数字(range()函数返回一个从 0 开始并以 1(默认)递增并在给定数字之前停止的数字序列)。
使用if 条件语句检查第一个和第二个数字是否都完全被索引 (i) 除以模运算符%(返回余数),and运算符(如果两个条件都为真则返回真)
如果条件为真,则将相应的索引 (i) 值分配为 HCF
打印输入数字 1 和输入数字 2 的结果 HCF。
示例
以下程序使用 for 循环返回输入数字 1 和输入数字 2 的 HCF –
# input number 1 inputNumber_1 = 30 # input number 2 inputNumber_2 = 14 # checking whether the first number is greater than # the second one if inputNumber_1 > inputNumber_2: # assigning the second number as a smaller number small_num = inputNumber_2 else: # else assigning the first number as a smaller number small_num = inputNumber_1 # traversing from 1 to the small number range for i in range(1, small_num+1): # checking whether both the first and second numbers divide exactly by index (i) if((inputNumber_1 % i == 0) and (inputNumber_2 % i == 0)): # assigning the index(i) value as HCF if the condition is true resultHcf = i # printing the HCF of input number 1 & input number 2 print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", resultHcf)
输出
执行上述程序后,将生成以下输出 –
The H.C.F of 30 and 14 = 2
使用欧几里得算法
示例
以下程序使用欧几里得算法返回输入数字 1 和输入数字 2 的 HCF –
# creating a function that returns the HCF of two numbers passed to it def calculateHcf(p, q): # traversing the loop until q times while(q): # assigning q value to p, p % q value to q p, q = q, p % q # returning p value(i.e, inputNumber_2) return p # input number 1 inputNumber_1 = 15 # input number 2 inputNumber_2 = 4 # calling the calculateHcf() function by passing both the input numbers to it resultHcf = calculateHcf(inputNumber_1, inputNumber_2) # printing the HCF of input number 1 & input number 2 print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", calculateHcf(inputNumber_1, inputNumber_2))
输出
执行上述程序后,将生成以下输出 –
The H.C.F of 15 and 4 = 1
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
使用 While 循环(重复减法)
示例
以下程序使用 while 循环(重复减法方法)返回输入数字 1 和输入数字 2 的 HCF –
# input number 1 inputNumber_1 = 40 # input number 2 inputNumber_2 = 5 # storing both the numbers in temporary variables # for not getting disturbed temp_1 = inputNumber_1 temp_2 = inputNumber_2 # traversing the loop until both the numbers are not equal while inputNumber_1 != inputNumber_2: # checking whether inputNumber_1 is greater than inputNumber_2 if inputNumber_1 > inputNumber_2: # assigning inputNumber_1-inputNumber_2 value to inputNumber_1 inputNumber_1 -= inputNumber_2 else: # else assigning inputNumber_2-inputNumber_1 value to inputNumber_2 inputNumber_2 -= inputNumber_1 # printing the HCF of input number 1 & input number 2 print("The H.C.F of", temp_1, "and", temp_2, "=", inputNumber_1)
输出
执行上述程序后,将生成以下输出 –
The H.C.F of 40 and 5 = 5
使用递归(朴素方法)
示例
以下程序使用递归返回输入数字 1 和输入数字 2 的 HCF –
# creating a function that returns the HCF of two numbers passed to it def calculateHcf(num_1, num_2): # checking whether the second number is equal to 0 if(num_2 == 0): # returning the absolute value of the first number if the condition is true return abs(num_1) else: # else returning the resultant HCF by calling the calculateHcf() function # by passing num_2, value of num_1 % num_2 as arguments return calculateHcf(num_2, num_1 % num_2) # input number 1 inputNumber_1 = 32 # input number 2 inputNumber_2 = 6 # calling the calculateHcf() function by passing both the input numbers to it print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", calculateHcf(inputNumber_1, inputNumber_2))
输出
执行上述程序后,将生成以下输出 –
The H.C.F of 32 and 6 = 2
处理 HCF 中的负数
两个数字的 HCF 永远不可能为负,因此,如果任何数字为负,只需将它们乘以 -1 使其变为正数。
在这种方法中,通过在欧几里得算法中有效地使用模运算符 (%),提高了重复减法的复杂度。
以下程序通过处理 HCF 中的负数,使用 while 返回输入数字 1 和输入数字 2 的 HCF –
示例
# creating a function to calculate the hcf def calculateHcf(x, y): return y == 0 and x or calculateHcf(y, x % y) # input number 1 inputNumber_1 = -4 # input number 2 inputNumber_2 = 15 # If the user types a negative number, we simply change it to a positive number. # HCF is the highest positive number that divides both numbers # -4 & 15 --> HCF = 1 (as the highest number that divides both) # -4 & -15 --> HCF = 1 (as the highest number that divides both) inputNumber_1 >= 0 and inputNumber_1 or -inputNumber_1 inputNumber_2 >= 0 and inputNumber_2 or -inputNumber_2 # calling the calculateHcf() function by passing both the input numbers to it print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", calculateHcf(inputNumber_1, inputNumber_2))
输出
执行上述程序后,将生成以下输出 –
The H.C.F of -4 and 15 = 1
结论
在本文中,我们学习了如何使用四种不同的方法在 Python 中查找数字的 gcd/hcf。我们还学习了在计算 HCF 时如何处理负数。