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/lcm/python/lcm.py b/algebra/lcm/python/lcm.py new file mode 100644 index 000000000..11cae1828 --- /dev/null +++ b/algebra/lcm/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 diff --git a/algebra/nth_root/python/nth_root.py b/algebra/nth_root/python/nth_root.py new file mode 100644 index 000000000..8efc73ab7 --- /dev/null +++ b/algebra/nth_root/python/nth_root.py @@ -0,0 +1,19 @@ +""" + Find the nth root of a and return the integer component only + @input: number, root + @output: nth root of number +""" +def nth_root(a,n): + high = 1 + while high ** n <= a: + high *= 2 + low = high/2 + while low < high: + mid = (low + high) // 2 + if low < mid and mid**n < a: + low = mid + elif high > mid and mid**n > a: + high = mid + else: + return mid + return mid + 1