Python gcd for list

redmouse picture redmouse · Mar 22, 2015 · Viewed 31.1k times · Source

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)

Answer

Ayoub ABOUNAKIF picture Ayoub ABOUNAKIF · Jun 13, 2018

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