-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPowXN.py
executable file
·37 lines (32 loc) · 932 Bytes
/
PowXN.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
# -*- coding: UTF-8 -*-
#
# Implement pow(x, n).
#
# Python 3 accepted. Python 2 gets a RuntimeError:
# maximum recursion depth exceeded while calling a Python object
class PowXN(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
# 🌝 ok, this may be the simplest way to solve the problem, but a little tricky.
# Python, Python 3 all accepted.
# return math.pow(x, n)
# 🌚 Another tricky way.
# Python, Python 3 all accepted.
# return x ** n
if n == 0:
return 1.0
if n == 1:
return x
if n > 0:
result = self.myPow(x, n // 2)
else:
result = self.myPow(x, int(n / 2))
if n % 2 == 0:
return result * result
elif n > 0:
return x * result * result
return (result * result) / x