Skip to content

Instantly share code, notes, and snippets.

@khaledsaikat
Last active July 13, 2018 21:54
Show Gist options
  • Save khaledsaikat/08228454b21d3360bf5e882d18a37fef to your computer and use it in GitHub Desktop.
Save khaledsaikat/08228454b21d3360bf5e882d18a37fef to your computer and use it in GitHub Desktop.
GCD and LCM in python for list
#!/usr/bin/env python3
from math import gcd
from functools import reduce
def lcm(a, b):
"""lowest common multiple of a and b"""
return int(a * b / gcd(a, b))
def gcd_from_list(arr):
"""Calculate greatest common divisor from list"""
return reduce(gcd, arr)
def lcm_from_list(arr):
"""Calculate lowest common multiple from list"""
return reduce(lcm, arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment