-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added iterative solution for power calculation #12709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
SajeevSenthil
wants to merge
8
commits into
TheAlgorithms:master
from
SajeevSenthil:optimized-power-iteration
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f1a39f3
Added iterative solution for power calculation
SajeevSenthil 89ad00e
Added iterative solution for power calculation
SajeevSenthil ee3f4be
Added iterative solution for power calculation
SajeevSenthil a55058b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1b86189
Added iterative solution for power calculation fixes #12709
SajeevSenthil 53ca0d3
Merge branch 'optimized-power-iteration' of https://github.com/Sajeev…
SajeevSenthil bde1393
Added iterative solution for power calculation FIXES NUMBER 12709
SajeevSenthil 935febe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
def power(base: float, exponent: int) -> float: | ||
""" | ||
Optimized power function using exponentiation by squaring. | ||
|
||
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, 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 | ||
exponent = -exponent | ||
while exponent: | ||
if exponent % 2 == 1: | ||
result *= base | ||
base *= base # Square the base | ||
exponent //= 2 # Halve the exponent | ||
return round(result, 5) # Round to 5 decimal places | ||
|
||
|
||
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 as e: | ||
# Handle invalid input or indeterminate cases | ||
print(e) | ||
except ZeroDivisionError as e: | ||
# Handle division by zero | ||
print(e) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.