diff --git a/algebra/gcd/python/gcd.py b/algebra/gcd/python/gcd.py new file mode 100644 index 000000000..a123e7011 --- /dev/null +++ b/algebra/gcd/python/gcd.py @@ -0,0 +1,14 @@ +""" + euclid_gcd(0 is used to find the gcd of two numbers using euclidean algorithm + @input: a, b numbers + @output gcd(a,b) +""" + +def euclid_gcd(a, b): + if a < b: + return euclid_gcd(b, a) + + while b != 0: + (a, b) = (b, a%b) + + return a diff --git a/algebra/gcd/python/lcm.py b/algebra/gcd/python/lcm.py new file mode 100644 index 000000000..11cae1828 --- /dev/null +++ b/algebra/gcd/python/lcm.py @@ -0,0 +1,23 @@ +""" + euclid_gcd() is used to find the gcd of two numbers using euclidean algorithm + @input: a, b numbers + @output gcd(a,b) +""" + +def euclid_gcd(a, b): + if a < b: + return euclid_gcd(b, a) + + while b != 0: + (a, b) = (b, a%b) + + return a +""" + lcm() is used to return the least common multiple between two numbers + @input: a, b numbers + @output: lcm(a, b) +""" + +def lcm(a, b): + gcd = euclid_gcd(a, b) + return (a*b)/gcd