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

25 lines
614 B
Python
Raw Permalink Normal View History

2023-11-13 12:47:53 +00:00
# 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