Skip to content

Commit a5e0616

Browse files
Modified power_of_n.py
1 parent 89ebaca commit a5e0616

File tree

1 file changed

+58
-57
lines changed

1 file changed

+58
-57
lines changed

power_of_n.py

+58-57
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
1-
#Python program to calculate x raised to the power n (i.e., x^n)
2-
3-
# Script Name : power_of_n.py
4-
# Author : Himanshu Gupta
5-
# Created : 2nd September 2023
6-
# Last Modified :
7-
# Version : 1.0
8-
# Modifications :
9-
# Description : Program which calculates x raised to the power of n, where x can be float number or integer and n can be positive or negative number
10-
# Example 1:
11-
12-
# Input: x = 2.00000, n = 10
13-
# Output: 1024.00000
14-
# Example 2:
15-
16-
# Input: x = 2.10000, n = 3
17-
# Output: 9.26100
18-
# Example 3:
19-
20-
# Input: x = 2.00000, n = -2
21-
# Output: 0.25000
22-
# Explanation: 2^-2 = 1/(2^2) = 1/4 = 0.25
23-
24-
#Class
25-
class Solution:
26-
27-
def binaryExponentiation(self, x: float, n: int) -> float:
28-
if n == 0:
29-
return 1
30-
31-
# Handle case where, n < 0.
32-
if n < 0:
33-
n = -1 * n
34-
x = 1.0 / x
35-
36-
# Perform Binary Exponentiation.
37-
result = 1
38-
while n != 0:
39-
# If 'n' is odd we multiply result with 'x' and reduce 'n' by '1'.
40-
if n % 2 == 1:
41-
result *= x
42-
n -= 1
43-
# We square 'x' and reduce 'n' by half, x^n => (x^2)^(n/2).
44-
x *= x
45-
n //= 2
46-
return result
47-
48-
49-
obj = Solution() #Creating object of the class Solution
50-
51-
#Taking inouts from the user
52-
x = float(input("Enter the base number: "))
53-
n = int(input("Enter the power number: "))
54-
55-
#calling the function using object obj to calculate the power
56-
answer = obj.binaryExponentiation(x, n)
57-
print(answer) #answer
1+
# Assign values to author and version.
2+
__author__ = "Himanshu Gupta"
3+
__version__ = "1.0.0"
4+
__date__ = "2023-09-03"
5+
6+
def binaryExponentiation(x: float, n: int) -> float:
7+
"""
8+
Function to calculate x raised to the power n (i.e., x^n) where x is a float number and n is an integer and it will return float value
9+
10+
Example 1:
11+
12+
Input: x = 2.00000, n = 10
13+
Output: 1024.0
14+
Example 2:
15+
16+
Input: x = 2.10000, n = 3
17+
Output: 9.261000000000001
18+
19+
Example 3:
20+
21+
Input: x = 2.00000, n = -2
22+
Output: 0.25
23+
Explanation: 2^-2 = 1/(2^2) = 1/4 = 0.25
24+
"""
25+
26+
if n == 0:
27+
return 1
28+
29+
# Handle case where, n < 0.
30+
if n < 0:
31+
n = -1 * n
32+
x = 1.0 / x
33+
34+
# Perform Binary Exponentiation.
35+
result = 1
36+
while n != 0:
37+
# If 'n' is odd we multiply result with 'x' and reduce 'n' by '1'.
38+
if n % 2 == 1:
39+
result *= x
40+
n -= 1
41+
# We square 'x' and reduce 'n' by half, x^n => (x^2)^(n/2).
42+
x *= x
43+
n //= 2
44+
return result
45+
46+
47+
if __name__ == "__main__":
48+
print(f"Author: {__author__}")
49+
print(f"Version: {__version__}")
50+
print(f"Function Documentation: {binaryExponentiation.__doc__}")
51+
print(f"Date: {__date__}")
52+
53+
print() # Blank Line
54+
55+
print(binaryExponentiation(2.00000, 10))
56+
print(binaryExponentiation(2.10000, 3))
57+
print(binaryExponentiation(2.00000, -2))
58+

0 commit comments

Comments
 (0)