From 715bc443b4cd207fe0d410551130603918434afa Mon Sep 17 00:00:00 2001 From: Garvit244 Date: Tue, 3 Oct 2017 11:52:12 +0800 Subject: [PATCH 1/3] Calculate GCD using euclidean algo and lcm --- algebra/gcd/python/gcd.py | 14 ++++++++++++++ algebra/gcd/python/lcm.py | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 algebra/gcd/python/gcd.py create mode 100644 algebra/gcd/python/lcm.py 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 From 3fe261040f86052fa84afe7363c1e3ec3e41bcf2 Mon Sep 17 00:00:00 2001 From: Garvit244 Date: Tue, 3 Oct 2017 12:10:04 +0800 Subject: [PATCH 2/3] Moving LCM to the correct directory --- algebra/{gcd => lcm}/python/lcm.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename algebra/{gcd => lcm}/python/lcm.py (100%) diff --git a/algebra/gcd/python/lcm.py b/algebra/lcm/python/lcm.py similarity index 100% rename from algebra/gcd/python/lcm.py rename to algebra/lcm/python/lcm.py From 3992cba18034d8a12b00b462904c43682f20cac1 Mon Sep 17 00:00:00 2001 From: Garvit244 Date: Tue, 3 Oct 2017 12:35:43 +0800 Subject: [PATCH 3/3] Finding the nth root in Python --- algebra/nth_root/python/nth_root.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 algebra/nth_root/python/nth_root.py 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