Skip to content

Commit c0beec5

Browse files
Added power_of_n.py
1 parent f1dd471 commit c0beec5

File tree

1 file changed

+0
-10
lines changed

1 file changed

+0
-10
lines changed

bit_manipulation/power_of_n.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
'''
22
Wikipedia explanation: https://en.wikipedia.org/wiki/Exponentiation
33
'''
4-
# Assign values to author and version.
5-
__author__ = "Himanshu Gupta"
6-
__version__ = "1.0.0"
7-
__date__ = "2023-09-03"
8-
9-
104
def binary_exponentiation(number: float, power: int) -> float:
115
"""
126
Function to calculate number raised to the power (i.e., number^power) where number is a float number and power is an integer and it will return float value
@@ -24,19 +18,15 @@ def binary_exponentiation(number: float, power: int) -> float:
2418
if power == 0:
2519
return 1
2620

27-
# Handle case where, power < 0.
2821
if power < 0:
2922
power = -1 * power
3023
number = 1.0 / number
3124

32-
# Perform Binary Exponentiation.
3325
result = 1
3426
while power != 0:
35-
# If 'power' is odd we multiply result with 'number' and reduce 'power' by '1'.
3627
if power % 2 == 1:
3728
result *= number
3829
power -= 1
39-
# We square 'number' and reduce 'power' by half, number^power => (number^2)^(power/2).
4030
number *= number
4131
power //= 2
4232
return result

0 commit comments

Comments
 (0)