Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I need to find the GCD for a list of numbers. I don't know, where I went wrong. Kindly look at 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)

1 Answer

0 votes
by (26.4k points)

Try the following code:

from fractions import gcd

from functools import reduce

def find_gcd(list):

    x = reduce(gcd, list)

    return x

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

Related questions

0 votes
4 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...