From 3992cba18034d8a12b00b462904c43682f20cac1 Mon Sep 17 00:00:00 2001 From: Garvit244 Date: Tue, 3 Oct 2017 12:35:43 +0800 Subject: [PATCH] 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