This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
kse-02/benchmark/exponentiation.py
github-classroom[bot] 24600885b9
Initial commit
2023-11-13 12:47:53 +00:00

24 lines
614 B
Python

# Based on https://github.com/AllAlgorithms, python/algorithms/math/exponentiation.py
def exponentiation(baseNumber: int, power: int) -> float:
assert not (baseNumber == 0 or power <= 0)
answer = None
if power > 1:
halfAnswer = exponentiation(baseNumber, power//2)
answer = halfAnswer * halfAnswer
if power%2 == 1:
answer *= baseNumber
elif power == 1:
answer = baseNumber
elif power == 0:
answer = 1
else: # negative power
answer = 1 / exponentiation(baseNumber, abs(power))
return answer