> For the complete documentation index, see [llms.txt](https://blog.bernardw.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.bernardw.com/problems/pow-x-n.md).

# Pow(x, n)

Implement pow(x, n).

{% tabs %}
{% tab title="Scala" %}

```scala
def myPow(_x: Double, _n: Int): Double = {
  var (x, n) = (_x, _n)
  if (x == 0) 0
  if (n == 0) 1
  else {
    if (n < 0) {
      x = 1 / x
      n = -n
    }
    var pow = 1
    while (n > 0) {
      if ((n & 1) == 1) {
        pow *= x
      }
      x *= x
      n <<= 1
    }
    pow
  }
}
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}
