Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing greatest common denominator in python

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)?

like image 216
D R Avatar asked Sep 04 '10 04:09

D R


People also ask

What is GCD and LCM in Python?

Write a Python program to Compute the greatest common divisor (GCD) and least common multiple (LCM) of two integer. This python program allows the user to enter two positive integer values and compute GCD using while loop.


1 Answers

One way to do it is:

import fractions

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

print gcd([4,8,12,24])
like image 101
D R Avatar answered Oct 01 '22 15:10

D R