Computes a^b iteratively, where a is the base and b is the exponent >>> binary_exp_iterative(3, 5) 243 >>> binary_exp_iterative(11, 13) 34522712143931 >>> binary_exp_iterative(-1, 3) -1 >>> binary_exp_iterative(0, 5) 0 >>> binary_exp_iterative(3, 1) 3
(base: float, exponent: int)
| 51 | |
| 52 | |
| 53 | def binary_exp_iterative(base: float, exponent: int) -> float: |
| 54 | """ |
| 55 | Computes a^b iteratively, where a is the base and b is the exponent |
| 56 | |
| 57 | >>> binary_exp_iterative(3, 5) |
| 58 | 243 |
| 59 | >>> binary_exp_iterative(11, 13) |
| 60 | 34522712143931 |
| 61 | >>> binary_exp_iterative(-1, 3) |
| 62 | -1 |
| 63 | >>> binary_exp_iterative(0, 5) |
| 64 | 0 |
| 65 | >>> binary_exp_iterative(3, 1) |
| 66 | 3 |
| 67 | >>> binary_exp_iterative(3, 0) |
| 68 | 1 |
| 69 | >>> binary_exp_iterative(1.5, 4) |
| 70 | 5.0625 |
| 71 | >>> binary_exp_iterative(3, -1) |
| 72 | Traceback (most recent call last): |
| 73 | ... |
| 74 | ValueError: Exponent must be a non-negative integer |
| 75 | """ |
| 76 | if exponent < 0: |
| 77 | raise ValueError("Exponent must be a non-negative integer") |
| 78 | |
| 79 | res: int | float = 1 |
| 80 | while exponent > 0: |
| 81 | if exponent & 1: |
| 82 | res *= base |
| 83 | |
| 84 | base *= base |
| 85 | exponent >>= 1 |
| 86 | |
| 87 | return res |
| 88 | |
| 89 | |
| 90 | def binary_exp_mod_recursive(base: float, exponent: int, modulus: int) -> float: |
nothing calls this directly
no outgoing calls
no test coverage detected