From b0bc67231484bdfbde5be58b3c957a22ab46648a Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:42:28 +0800 Subject: [PATCH 1/6] Add new algorithm Finding kth root of a number up to a given decimal places without using ** or pow() --- maths/finding_kth_root.py | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 maths/finding_kth_root.py diff --git a/maths/finding_kth_root.py b/maths/finding_kth_root.py new file mode 100644 index 000000000000..a8143f1c6af4 --- /dev/null +++ b/maths/finding_kth_root.py @@ -0,0 +1,73 @@ +""" +Finding the real root of a given degree of a given number, rounding to the nearest given number of decimal places without using ** + +Input: + - number: 3 + - deg: 2 + - decimal_place: 5 +Output: + - 1.73205 +""" + +def finding_kth_root(number: float, deg: int, decimal_place: int) -> float: + """ + Implementing binary search to find the kth root of a number + Negative numbers are treated the same way as positive numbers, with the minus sign added at the end + + >>> finding_kth_root(3, 2, 5) + 1.73205 + >>> finding_kth_root(122.683, 10, 7) + 1.6176272 + >>> finding_kth_root(5, 1, 3) + 5.0 + >>> finding_kth_root(0, 2, 2) + 0.0 + >>> finding_kth_root(125.7, 2, 0) + 11.0 + >>> finding_kth_root(-123, 5, 3) + -2.618 + >>> finding_kth_root(-123, 4, 3) + Traceback (most recent call last): + ... + ValueError: Cannot calculate real root of an even degree of a negative number. + >>> finding_kth_root(123, -1, 3) + Traceback (most recent call last): + ... + ValueError: Degree of the root must be at least 1. + + """ + if deg < 1: + raise ValueError("Degree of the root must be at least 1.") + + if deg % 2 == 0 and number < 0: + raise ValueError("Cannot calculate real root of an even degree of a negative number.") + + hi, lo = number, 0 + if number < 0: + hi = -number + + error = 1 + for i in range(decimal_place+1): + error /= 10 + + while(hi - lo >= error): #Precision is not reached, continue looping + mid = (hi + lo) / 2 + product = 1 + for i in range(deg): + product *= mid + + if product > abs(number): #Overestimation, higher bound decreases to mid + hi = mid + + else: #Underestimation, lower bound increases to mid + lo = mid + + if number < 0: + return -round(lo, decimal_place) + + return round(lo, decimal_place) + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 897f8e5966a3563261b83f863860c1e84434a575 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:46:55 +0800 Subject: [PATCH 2/6] Change all int assignment to float --- maths/finding_kth_root.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/maths/finding_kth_root.py b/maths/finding_kth_root.py index a8143f1c6af4..469ee5368b51 100644 --- a/maths/finding_kth_root.py +++ b/maths/finding_kth_root.py @@ -1,5 +1,6 @@ """ -Finding the real root of a given degree of a given number, rounding to the nearest given number of decimal places without using ** +Finding the real root of a given degree of a given number +rounding to the nearest given number of decimal places without using ** or pow() Input: - number: 3 @@ -11,8 +12,9 @@ def finding_kth_root(number: float, deg: int, decimal_place: int) -> float: """ - Implementing binary search to find the kth root of a number - Negative numbers are treated the same way as positive numbers, with the minus sign added at the end + Implementing binary search to find the kth root of a number. + Negative numbers are treated the same way as positive numbers, + with the minus sign added at the end. >>> finding_kth_root(3, 2, 5) 1.73205 @@ -42,17 +44,17 @@ def finding_kth_root(number: float, deg: int, decimal_place: int) -> float: if deg % 2 == 0 and number < 0: raise ValueError("Cannot calculate real root of an even degree of a negative number.") - hi, lo = number, 0 + hi, lo = number, 0.0 if number < 0: hi = -number - error = 1 + error = 1.0 for i in range(decimal_place+1): error /= 10 while(hi - lo >= error): #Precision is not reached, continue looping mid = (hi + lo) / 2 - product = 1 + product = 1.0 for i in range(deg): product *= mid From 30daa416490ef75b897bf8e543f579c553deab16 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:52:25 +0800 Subject: [PATCH 3/6] Update finding_kth_root.py --- maths/finding_kth_root.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/finding_kth_root.py b/maths/finding_kth_root.py index 469ee5368b51..a6ec6e8c20df 100644 --- a/maths/finding_kth_root.py +++ b/maths/finding_kth_root.py @@ -2,6 +2,8 @@ Finding the real root of a given degree of a given number rounding to the nearest given number of decimal places without using ** or pow() +[Problem link] https://www.geeksforgeeks.org/calculating-n-th-real-root-using-binary-search/ + Input: - number: 3 - deg: 2 From becf43dedf9923b26801d5990e8165c083fd95b2 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:52:37 +0800 Subject: [PATCH 4/6] Update finding_kth_root.py --- maths/finding_kth_root.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/finding_kth_root.py b/maths/finding_kth_root.py index a6ec6e8c20df..ef3dffa14267 100644 --- a/maths/finding_kth_root.py +++ b/maths/finding_kth_root.py @@ -2,7 +2,8 @@ Finding the real root of a given degree of a given number rounding to the nearest given number of decimal places without using ** or pow() -[Problem link] https://www.geeksforgeeks.org/calculating-n-th-real-root-using-binary-search/ +[Problem link] +https://www.geeksforgeeks.org/calculating-n-th-real-root-using-binary-search/ Input: - number: 3 From fa8121ff3b21873fbd06e75f32d8e36d3f448cf2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:54:31 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/finding_kth_root.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/maths/finding_kth_root.py b/maths/finding_kth_root.py index ef3dffa14267..dabd07cb79eb 100644 --- a/maths/finding_kth_root.py +++ b/maths/finding_kth_root.py @@ -2,7 +2,7 @@ Finding the real root of a given degree of a given number rounding to the nearest given number of decimal places without using ** or pow() -[Problem link] +[Problem link] https://www.geeksforgeeks.org/calculating-n-th-real-root-using-binary-search/ Input: @@ -13,12 +13,13 @@ - 1.73205 """ + def finding_kth_root(number: float, deg: int, decimal_place: int) -> float: """ Implementing binary search to find the kth root of a number. - Negative numbers are treated the same way as positive numbers, + Negative numbers are treated the same way as positive numbers, with the minus sign added at the end. - + >>> finding_kth_root(3, 2, 5) 1.73205 >>> finding_kth_root(122.683, 10, 7) @@ -39,40 +40,43 @@ def finding_kth_root(number: float, deg: int, decimal_place: int) -> float: Traceback (most recent call last): ... ValueError: Degree of the root must be at least 1. - + """ if deg < 1: - raise ValueError("Degree of the root must be at least 1.") + raise ValueError("Degree of the root must be at least 1.") if deg % 2 == 0 and number < 0: - raise ValueError("Cannot calculate real root of an even degree of a negative number.") - + raise ValueError( + "Cannot calculate real root of an even degree of a negative number." + ) + hi, lo = number, 0.0 if number < 0: hi = -number - + error = 1.0 - for i in range(decimal_place+1): + for i in range(decimal_place + 1): error /= 10 - while(hi - lo >= error): #Precision is not reached, continue looping + while hi - lo >= error: # Precision is not reached, continue looping mid = (hi + lo) / 2 product = 1.0 - for i in range(deg): + for i in range(deg): product *= mid - if product > abs(number): #Overestimation, higher bound decreases to mid + if product > abs(number): # Overestimation, higher bound decreases to mid hi = mid - - else: #Underestimation, lower bound increases to mid + + else: # Underestimation, lower bound increases to mid lo = mid - + if number < 0: return -round(lo, decimal_place) - + return round(lo, decimal_place) + if __name__ == "__main__": import doctest - + doctest.testmod() From 753512e8babf9baba7f94184c93f4dce0f0bfce9 Mon Sep 17 00:00:00 2001 From: hollowcrust <72879387+hollowcrust@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:58:44 +0800 Subject: [PATCH 6/6] Change i to _i --- maths/finding_kth_root.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/finding_kth_root.py b/maths/finding_kth_root.py index dabd07cb79eb..31ed25a2bf1f 100644 --- a/maths/finding_kth_root.py +++ b/maths/finding_kth_root.py @@ -55,13 +55,13 @@ def finding_kth_root(number: float, deg: int, decimal_place: int) -> float: hi = -number error = 1.0 - for i in range(decimal_place + 1): + for _i in range(decimal_place + 1): error /= 10 while hi - lo >= error: # Precision is not reached, continue looping mid = (hi + lo) / 2 product = 1.0 - for i in range(deg): + for _i in range(deg): product *= mid if product > abs(number): # Overestimation, higher bound decreases to mid