How to calculate the GCD (greatest common divisor) of a list of numbers in Python

2 Answers

0 votes
import math

lst = [9, 12, 24, 27, 30, 36, 90]

print(math.gcd(*lst))
    

    
'''
run:
     
3

'''

 



answered Feb 18, 2023 by avibootz
0 votes
import math
from functools import reduce

def calculate_gcd(lst):
    return reduce(math.gcd, lst)

lst = [9, 12, 24, 27, 30, 36, 90]

print(calculate_gcd(lst))
    

    
'''
run:
     
3

'''

 



answered Feb 18, 2023 by avibootz
...