forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower.py
More file actions
Latest commit
33 lines (29 loc) · 686 Bytes
/
Copy pathpower.py
File metadata and controls
33 lines (29 loc) · 686 Bytes
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
defactual_power(a: int, b: int):
"""
Function using divide and conquer to calculate a^b.
It only works for integer a,b.
"""
ifb==0:
return1
if (b%2) ==0:
returnactual_power(a, int(b/2)) *actual_power(a, int(b/2))
else:
returna*actual_power(a, int(b/2)) *actual_power(a, int(b/2))
defpower(a: int, b: int) ->float:
"""
>>> power(4,6)
4096
>>> power(2,3)
8
>>> power(-2,3)
-8
>>> power(2,-3)
0.125
>>> power(-2,-3)
-0.125
"""
ifb<0:
return1/actual_power(a, b)
returnactual_power(a, b)
if__name__=="__main__":
print(power(-2, -3))