-
Notifications
You must be signed in to change notification settings - Fork 0
/
048 Pow(x, n).py
executable file
·77 lines (62 loc) · 1.51 KB
/
048 Pow(x, n).py
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
74
75
76
77
"""
Implement pow(x, n).
"""
__author__ = 'Danyang'
class Solution:
def pow(self, x, n):
"""
O(log n)
Algorithm: math, Exponentiation by Squaring
Basically: x^n = (x^2)^(n/2)
More formally: x^n = x^(n/2) * x^(n/2) * x^(n%2)
:param x: float
:param n: integer
:return: float
"""
invert_flag = False if n > 0 else True
# O(log n)
n = abs(n)
product = 1.0
while n > 0:
if n & 1 == 1:
product *= x
n = n >> 1
x *= x
if invert_flag:
product = 1.0 / product
return product
# @param x, a float
# @param n, a integer
# @return a float
def pow_TLE(self, x, n):
"""
O(n)
"""
if abs(x)<=0.00001:
return 0
if x==1.0:
return 1
if x==-1.0:
if n&1==1:
return 1
else:
return -1
if abs(x-1.0)<1e-6:
return 1+(x-1.0)*n
if abs(x--1.0)<1e-6:
if n % 2==0:
return self.pow(-x, n)
else:
return -self.pow(-x, n)
product = 1.0
for i in xrange(abs(n)):
pre = product
if n>0:
product *= x
else:
product /= x
if abs(product - pre)<1e-5:
break
return product
if __name__=="__main__":
print Solution().pow(8.88023, 3)