How do you find the gcd of 3 numbers in python?

Very simple.
Write a function, that calculates gcd/lcm of two numbers.
Then do something like this.

gcd[a,b,c] = gcd[a, gcd[b,c]]

 >>> def gcd[a,b]:
...     if b == 0:
...             return a
...     else:
...             return gcd[b, a%b]
... 
>>> gcd[3,5]
1
>>> gcd[10,5]
5
>>> gcd[10,15]
5
>>> gcd[5,gcd[10,15]]
5

You can try by yourself, for lcm.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers.

    gcd[a, b, c] = gcd[a, gcd[b, c]] 
                 = gcd[gcd[a, b], c] 
                 = gcd[gcd[a, c], b]
    

    def find_gcd[x, y]:

        while[y]:

            x, y = y, x % y

        return x

    l = [2, 4, 6, 8, 16]

    num1=l[0]

    num2=l[1]

    gcd=find_gcd[num1,num2]

    for i in range[2,len[l]]:

        gcd=find_gcd[gcd,l[i]]

    print[gcd]

    Output:

    2
    

    Please refer complete article on GCD of more than two [or array] numbers for more details!

    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The Highest Common Factor [HCF], also called gcd, can be computed in python using a single function offered by math module and hence can make tasks easier in many situations.

    Naive Methods to compute gcd

    Way 1: Using Recursion

    Python3

    def hcfnaive[a, b]:

        if[b == 0]:

            return abs[a]

        else:

            return hcfnaive[b, a % b]

    a = 60

    b = 48

    print["The gcd of 60 and 48 is : ", end=""]

    print[hcfnaive[60, 48]]

    Output

    The gcd of 60 and 48 is : 12
    

    Way 2: Using Loops 

    Python3

    def computeGCD[x, y]:

        if x > y:

            small = y

        else:

            small = x

        for i in range[1, small + 1]:

            if[[x % i == 0] and [y % i == 0]]:

                gcd = i

        return gcd

    a = 60

    b = 48

    print ["The gcd of 60 and 48 is : ", end=""]

    print [computeGCD[60,48]]

    Output

    The gcd of 60 and 48 is : 12
    

    Way 3: Using Euclidean Algorithm 

    Python3

    def computeGCD[x, y]:

       while[y]:

           x, y = y, x % y

        return abs[x]

    a = 60

    b = 48

    print ["The gcd of 60 and 48 is : ",end=""]

    print [computeGCD[60, 48]]

    Output:

    The gcd of 60 and 48 is : 12
    • Both numbers are 0, gcd is 0
    • If only either number is Not a number, Type Error is raised.

    How do you find the gcd of 3 numbers?

    The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers.

    How do you calculate gcd in Python?

    gcd[] function compute the greatest common divisor of 2 numbers mentioned in its arguments..
    Syntax: math.gcd[x, y].
    Parameter:.
    x : Non-negative integer whose gcd has to be computed..
    y : Non-negative integer whose gcd has to be computed..

    Chủ Đề