Computing greatest common denominator in python

与世无争的帅哥 提交于 2019-12-19 03:24:19

问题


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:


One way to do it is:

import fractions

def gcd(L):
    return reduce(fractions.gcd, L)

print gcd([4,8,12,24])


来源:https://stackoverflow.com/questions/3640955/computing-greatest-common-denominator-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!