Update binary_exponentiation.py (#10342)

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
This commit is contained in:
Iyiola Aloko 2023-10-19 08:21:48 -04:00 committed by GitHub
parent 30c8d5573a
commit b301e589e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,10 +6,21 @@
def binary_exponentiation(a: int, n: int) -> int: def binary_exponentiation(a: int, n: int) -> int:
""" """
Compute a number raised by some quantity
>>> binary_exponentiation(-1, 3)
-1
>>> binary_exponentiation(-1, 4)
1
>>> binary_exponentiation(2, 2)
4
>>> binary_exponentiation(3, 5) >>> binary_exponentiation(3, 5)
243 243
>>> binary_exponentiation(10, 3) >>> binary_exponentiation(10, 3)
1000 1000
>>> binary_exponentiation(5e3, 1)
5000.0
>>> binary_exponentiation(-5e3, 1)
-5000.0
""" """
if n == 0: if n == 0:
return 1 return 1
@ -28,7 +39,7 @@ if __name__ == "__main__":
doctest.testmod() doctest.testmod()
try: try:
BASE = int(input("Enter Base : ").strip()) BASE = int(float(input("Enter Base : ").strip()))
POWER = int(input("Enter Power : ").strip()) POWER = int(input("Enter Power : ").strip())
except ValueError: except ValueError:
print("Invalid literal for integer") print("Invalid literal for integer")