mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Update binary_exponentiation.py (#10342)
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
This commit is contained in:
parent
30c8d5573a
commit
b301e589e2
|
@ -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")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user