From f1a39f37def4b9b0a59cd8289a62ff62472cc96f Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 13:06:01 +0530 Subject: [PATCH 1/7] Added iterative solution for power calculation --- maths/power_using_iteration.py | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 maths/power_using_iteration.py diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py new file mode 100644 index 000000000000..023fe5f32a6f --- /dev/null +++ b/maths/power_using_iteration.py @@ -0,0 +1,85 @@ +""" +Iterative solution to calculate the power of a base raised to an exponent. + +This implementation uses an iterative approach, unlike the recursive approach +in `power_using_recursion.py`. The algorithm is based on exponentiation by squaring +for optimal performance. + +Examples: + >>> power(2, 3) + 8 + >>> power(5, -2) + 0.04 + >>> power(10, 0) + 1 + >>> Failed example: + power(5, -2) + Expected: + 0.04 + Got: + 0.04000000000000001 + 1 items had failures: + 1 of 3 in __main__ + 1 failed in total + Raise base to the power of exponent using an optimized approach... + Enter the base: 500 + Enter the exponent: 8 + 500.0 to the power of 8 is 3.90625e+21 + +Input: + base (float): The base number (can be integer or float). + exponent (int): The exponent (can be positive, negative, or zero). + +Output: + float: The result of base raised to the power of exponent. + +Note: + Results for very large or very small floating-point numbers may have slight precision errors + due to the limitations of floating-point arithmetic. +""" + + +def power(base: float, exponent: int) -> float: + """ + Optimized power function using exponentiation by squaring. + It handles both positive and negative exponents efficiently. + This function take time complexity O(log n) for exponentiation. + space complexity is O(1) as it uses a constant amount of space. + """ + # Handle negative exponents by taking reciprocal of the base + if exponent < 0: + base = 1 / base + exponent = -exponent + + result = 1 + # Use exponentiation by squaring for efficiency + while exponent: + if exponent % 2 == 1: # If the current exponent is odd + result *= base + base *= base # Square the base + exponent //= 2 # Halve the exponent + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print("Raise base to the power of exponent using an optimized approach...") + + try: + # Input handling and validation + base = float(input("Enter the base: ").strip()) # Supports float & int + exponent = int( + input("Enter the exponent: ").strip() + ) # Ensures exponent is an integer + + # Calculate result + result = power(base, exponent) + + # Display the result + print(f"{base} to the power of {exponent} is {result}") + + except ValueError: + # Handle invalid input + print("Invalid input! Please enter numeric values for base and exponent.") From 89ad00e4433968d40bd6129c9d7dddb1c775ce9b Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 13:20:38 +0530 Subject: [PATCH 2/7] Added iterative solution for power calculation --- maths/power_using_iteration.py | 43 +--------------------------------- 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index 023fe5f32a6f..cd92b9a324a1 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -1,44 +1,3 @@ -""" -Iterative solution to calculate the power of a base raised to an exponent. - -This implementation uses an iterative approach, unlike the recursive approach -in `power_using_recursion.py`. The algorithm is based on exponentiation by squaring -for optimal performance. - -Examples: - >>> power(2, 3) - 8 - >>> power(5, -2) - 0.04 - >>> power(10, 0) - 1 - >>> Failed example: - power(5, -2) - Expected: - 0.04 - Got: - 0.04000000000000001 - 1 items had failures: - 1 of 3 in __main__ - 1 failed in total - Raise base to the power of exponent using an optimized approach... - Enter the base: 500 - Enter the exponent: 8 - 500.0 to the power of 8 is 3.90625e+21 - -Input: - base (float): The base number (can be integer or float). - exponent (int): The exponent (can be positive, negative, or zero). - -Output: - float: The result of base raised to the power of exponent. - -Note: - Results for very large or very small floating-point numbers may have slight precision errors - due to the limitations of floating-point arithmetic. -""" - - def power(base: float, exponent: int) -> float: """ Optimized power function using exponentiation by squaring. @@ -58,7 +17,7 @@ def power(base: float, exponent: int) -> float: result *= base base *= base # Square the base exponent //= 2 # Halve the exponent - return result + return round(result, 5) # Round to 5 decimal places if __name__ == "__main__": From ee3f4be0b848b4a431d549902024ece53fb082f0 Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 15:43:59 +0530 Subject: [PATCH 3/7] Added iterative solution for power calculation --- maths/power_using_iteration.py | 47 +++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index cd92b9a324a1..916a8ecbf9ba 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -1,19 +1,49 @@ def power(base: float, exponent: int) -> float: """ Optimized power function using exponentiation by squaring. - It handles both positive and negative exponents efficiently. - This function take time complexity O(log n) for exponentiation. - space complexity is O(1) as it uses a constant amount of space. + + Args: + base (float): The base number. + exponent (int): The exponent. + + Returns: + float: The result of base raised to the power of exponent. + + Examples: + >>> power(2, 3) + 8.0 + >>> power(5, -2) + 0.04 + >>> power(10, 0) + 1.0 + >>> power(7, 2) + 49.0 + >>> power(2, -3) + 0.125 + >>> power(2.5, 4) + 39.0625 + >>> power(-3.5, 2) + 12.25 + >>> power(-2, 3) + -8.0 + >>> power(0, 5) + 0.0 + >>> power(0, 0) + 1.0 + >>> power(0, -1) + Traceback (most recent call last): + ... + ZeroDivisionError: 0.0 cannot be raised to a negative power. + >>> power(1, 1000) + 1.0 + """ - # Handle negative exponents by taking reciprocal of the base + result = 1.0 if exponent < 0: base = 1 / base exponent = -exponent - - result = 1 - # Use exponentiation by squaring for efficiency while exponent: - if exponent % 2 == 1: # If the current exponent is odd + if exponent % 2 == 1: result *= base base *= base # Square the base exponent //= 2 # Halve the exponent @@ -22,7 +52,6 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": import doctest - doctest.testmod() print("Raise base to the power of exponent using an optimized approach...") From a55058b80b94508234336693141d73a23006b3d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 10:14:28 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/power_using_iteration.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index 916a8ecbf9ba..322771b86830 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -24,7 +24,7 @@ def power(base: float, exponent: int) -> float: 39.0625 >>> power(-3.5, 2) 12.25 - >>> power(-2, 3) + >>> power(-2, 3) -8.0 >>> power(0, 5) 0.0 @@ -34,9 +34,9 @@ def power(base: float, exponent: int) -> float: Traceback (most recent call last): ... ZeroDivisionError: 0.0 cannot be raised to a negative power. - >>> power(1, 1000) + >>> power(1, 1000) 1.0 - + """ result = 1.0 if exponent < 0: @@ -52,6 +52,7 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": import doctest + doctest.testmod() print("Raise base to the power of exponent using an optimized approach...") From 1b86189d40c46ce8ac649df1259e959f4ae3de2b Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 15:54:29 +0530 Subject: [PATCH 5/7] Added iterative solution for power calculation fixes #12709 --- maths/power_using_iteration.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index 916a8ecbf9ba..bd79fa515ab6 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -28,16 +28,25 @@ def power(base: float, exponent: int) -> float: -8.0 >>> power(0, 5) 0.0 - >>> power(0, 0) - 1.0 + >>> power(0, 1) + 0.0 >>> power(0, -1) Traceback (most recent call last): ... ZeroDivisionError: 0.0 cannot be raised to a negative power. + >>> power(0, 0) + Traceback (most recent call last): + ... + ValueError: 0.0 raised to the power of 0 is indeterminate. >>> power(1, 1000) 1.0 """ + if base == 0 and exponent == 0: + raise ValueError("0.0 raised to the power of 0 is indeterminate.") + if base == 0 and exponent < 0: + raise ZeroDivisionError("0.0 cannot be raised to a negative power.") + result = 1.0 if exponent < 0: base = 1 / base @@ -51,6 +60,7 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": + import doctest doctest.testmod() print("Raise base to the power of exponent using an optimized approach...") @@ -68,6 +78,9 @@ def power(base: float, exponent: int) -> float: # Display the result print(f"{base} to the power of {exponent} is {result}") - except ValueError: - # Handle invalid input - print("Invalid input! Please enter numeric values for base and exponent.") + except ValueError as e: + # Handle invalid input or indeterminate cases + print(e) + except ZeroDivisionError as e: + # Handle division by zero + print(e) From bde13935f18372f64197a23d7e3257c5b09d2689 Mon Sep 17 00:00:00 2001 From: SajeevSenthil <167018420+SajeevSenthil@users.noreply.github.com> Date: Tue, 6 May 2025 16:01:31 +0530 Subject: [PATCH 6/7] Added iterative solution for power calculation FIXES NUMBER 12709 --- maths/power_using_recursion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py index eb775b161ae8..c560503525a3 100644 --- a/maths/power_using_recursion.py +++ b/maths/power_using_recursion.py @@ -60,4 +60,4 @@ def power(base: int, exponent: int) -> float: result = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents result = 1 / result - print(f"{base} to the power of {exponent} is {result}") + print(f"{base} to the power of {exponent} is {result}") \ No newline at end of file From 935febedb187032e8bd8cc79be0a861acf100d30 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 10:40:32 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/power_using_iteration.py | 3 +-- maths/power_using_recursion.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/power_using_iteration.py b/maths/power_using_iteration.py index c14208af4b3f..f86b6454f4c3 100644 --- a/maths/power_using_iteration.py +++ b/maths/power_using_iteration.py @@ -46,7 +46,7 @@ def power(base: float, exponent: int) -> float: raise ValueError("0.0 raised to the power of 0 is indeterminate.") if base == 0 and exponent < 0: raise ZeroDivisionError("0.0 cannot be raised to a negative power.") - + result = 1.0 if exponent < 0: base = 1 / base @@ -60,7 +60,6 @@ def power(base: float, exponent: int) -> float: if __name__ == "__main__": - import doctest doctest.testmod() diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py index c560503525a3..eb775b161ae8 100644 --- a/maths/power_using_recursion.py +++ b/maths/power_using_recursion.py @@ -60,4 +60,4 @@ def power(base: int, exponent: int) -> float: result = power(base, abs(exponent)) if exponent < 0: # power() does not properly deal w/ negative exponents result = 1 / result - print(f"{base} to the power of {exponent} is {result}") \ No newline at end of file + print(f"{base} to the power of {exponent} is {result}")