-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Power using recursion: correctly implemented recursion #5050
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
Conversation
Multiple Pull Request Detected@matssson, we are extremely excited that you want to submit multiple algorithms in this repository but we have a limit on how many pull request a user can keep open at a time. This is to make sure all maintainers and users focus on a limited number of pull requests at a time to maintain the quality of the code. This pull request is being closed as the user already has an open pull request. Please focus on your previous pull request before opening another one. Thank you for your cooperation. User opened pull requests (including this one): #5050, #5048, #5045, #5043 |
The test failed since we threw a ValueError for 0^0, where it compared it to pythons pow function, which returns 1. |
@@ -13,7 +13,7 @@ | |||
""" | |||
|
|||
|
|||
def power(base: int, exponent: int) -> float: | |||
def power(base: int, exponent: int) -> int: | |||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test for 0^0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I removed the check for 0^0 since the testing suite expected it to return 1, which math.pow does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may reference those fast exp in the same folder and see what are their behaviour. It's still ok to add tests for this function.
@@ -23,7 +23,14 @@ def power(base: int, exponent: int) -> float: | |||
... for base in range(-10, 10) for exponent in range(10)) | |||
True | |||
""" | |||
return base * power(base, (exponent - 1)) if exponent else 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this works for 0^0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several https://github.com/TheAlgorithms/Python/blob/master/maths/binary_exponentiation.py https://github.com/TheAlgorithms/Python/blob/master/maths/binary_exponentiation_2.py for fast exp. Please use the original version but add a few more tests
Describe your change:
We make the recursion many times faster by calculating (base*base)^exponent/2 instead of base * base ^ (exponent-1).
We also throw a valueerror for 0^0.Checklist:
Fixes: #{$ISSUE_NO}
.