Consolidate binary exponentiation files#10742
Consolidate binary exponentiation files#10742tianyizheng02 merged 6 commits intoTheAlgorithms:masterfrom
Conversation
|
|
||
| def binary_exp_recursive(base: int, exponent: int) -> int: | ||
| """ | ||
| Compute a number raised by some quantity |
There was a problem hiding this comment.
Why remove this? exp is a bit cryptic in the function name and a line of documentation here is useful.
| """ | ||
| Compute a number raised by some quantity | ||
| >>> binary_exponentiation(-1, 3) | ||
| >>> binary_exp_recursive(3, 5) |
There was a problem hiding this comment.
What about tests for a=big number and n=big number, a=float, n=float?
There was a problem hiding this comment.
Can do, but the n=float case won't work because the algorithm can only compute integer powers
There was a problem hiding this comment.
I know it won’t work but I wanted to see how it fails. Does it raise a ValueError? Does it behave like pow() does. Testing how things work if half the battle. Watching them fail is just as cool.
maths/binary_exponentiation.py
Outdated
| RESULT = binary_exponentiation(BASE, POWER) | ||
| print(f"{BASE}^({POWER}) : {RESULT}") | ||
| RESULT = binary_exp_recursive(BASE, POWER) | ||
| print(f"{BASE}^({POWER}): {RESULT}") |
There was a problem hiding this comment.
OPTIONAL: Adding a timeit benchmark would be a nice addition. Visitors love to see the side-by-side bakeoff.
Describe your change:
Contributes to #8098
Consolidated implementations of binary (modular) exponentiation into just
binary_exponentiation.pyChecklist: