I want to calculate gcd for a list of numbers. But I don't know what's wrong with my code.
A = [12, 24, 27, 30, 36] def Greatest_Common_Divisor(A): for c in A: while int(c) > 0: if int(c) > 12: c = int(c) % 12 else: return 12 % int(c) print Greatest_Common_Divisor(A)
num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) # printing the result for the users. print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num1, num2))
gcd() method returns the greatest common divisor of the two integers int1 and int2. GCD is the largest common divisor that divides the numbers without a remainder. GCD is also known as the highest common factor (HCF). Tip: gcd(0,0) returns 0.
here is the piece of code, that I used:
from fractions import gcd from functools import reduce def find_gcd(list): x = reduce(gcd, list) return x
As of python 3.9, python got built-in support for calculating gcd over a list of numbers.
import math A = [12, 24, 27, 30, 36] print(math.gcd(*A))
Output:
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With