---
# System prepended metadata

title: 'Pow(x,n) <img src="https://img.shields.io/badge/-easy-brightgreen">'
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Pow(x,n) <img src="https://img.shields.io/badge/-easy-brightgreen">

Implement pow(x, n), which calculates x raised to the power n $(x^n)$.

**Example 1:**
```
Input: 2.00000, 10
Output: 1024.00000
```
**Example 2:**
```
Input: 2.10000, 3
Output: 9.26100
```
**Example 3:**
```
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
```
**Note:**

- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [$−2^{31}$, $2^{31} − 1$]

# Solution

### Solution 1: Recursive

```python=
class Solution:
    def myPow(self, x: float, n: int) -> float:
        if n > 0:
            return self.__myPow(x, n)
        else:
            return self.__myPow(1/x, -n)
        
    def __myPow(self, x, n):
        if n == 0:
            return 1
        else:
            part = self.__myPow(x, n//2)
            if n % 2 == 0:
                return part * part
            else:
                return part * part * x
```
