-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathpower_using_recursion.py
More file actions
73 lines (64 loc) · 1.73 KB
/
power_using_recursion.py
File metadata and controls
73 lines (64 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
== Raise base to the power of exponent using recursion ==
Input -->
Enter the base: 3
Enter the exponent: 4
Output -->
3 to the power of 4 is 81
Input -->
Enter the base: 2
Enter the exponent: 0
Output -->
2 to the power of 0 is 1
"""
def power(base: float, exponent: int) -> float:
"""
Calculate the power of a base raised to an exponent using recursion.
Args:
base (int): The base number.
exponent (int): The exponent number.
Returns:
the calculated value of base^exponent || base**exponent
cases:
>>> power(3, 4)
81
>>> power(2, 0)
1
>>> power(5, 1)
5
>>> power(2, -1)
0.5
>>> power(-2, 3)
-8
>>> power(-2, 2)
4
>>> power(0, 5)
0
>>> power (0,0)
1
"""
if not isinstance(exponent, int):
raise TypeError("exponent must be an integer")
if exponent == 0:
return 1
if exponent < 0:
return 1 / power(base, -exponent)
return base * power(base, exponent - 1)
if __name__ == "__main__":
from doctest import testmod
testmod()
print("Raise base to the power of exponent using recursion...")
try:
base = float(input("Enter the base: ").strip())
exponent = int(input("Enter the exponent: ").strip())
result = power(base, exponent)
print(f"{base} to the power of {exponent} is {result}")
except ValueError as e:
print(f"Invalid input: {e} ")
except TypeError as e:
print(f"error: {e}")
except RecursionError:
print(
"""error: Maximum recursive depth exceeded.
The exponent you might have given as input might have been very large"""
)