Skip to content

Instantly share code, notes, and snippets.

@kanglicheng
Created July 24, 2017 22:39
Show Gist options
  • Save kanglicheng/f196d251ad68e51253c1afdc20648a49 to your computer and use it in GitHub Desktop.
Save kanglicheng/f196d251ad68e51253c1afdc20648a49 to your computer and use it in GitHub Desktop.
calculating gcd and lcm of two numbers, using reduce for multiple numbers
#using python 3.5
import functools
# standard euclidean algorithm
def gcd(a, b):
while b:
a, b = b, a%b
return a
# with explicit recursion
def gcd_multiple(a, b):
if b==0:
return a
else:
return gcd_multiple(b, a%b)
# calculating the gcd of more than two numbers
def gcd_multiple(*args):
return functools.reduce(lambda x, y:gcd(x, y), args)
#modify to accomodate numbers in a list (array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment