Computing greatest common denominator in python

后端 未结 1 396
我寻月下人不归
我寻月下人不归 2021-01-05 15:51

If you have a list of integers in python, say L = [4,8,12,24], how can you compute their greatest common denominator/divisor (4 in this case)?

1条回答
  •  借酒劲吻你
    2021-01-05 16:10

    One way to do it is:

    import fractions
    
    def gcd(L):
        return reduce(fractions.gcd, L)
    
    print gcd([4,8,12,24])
    

    0 讨论(0)
提交回复
热议问题