Rating: 4.0


(We just skip the brute force at the beginning).

Simply, it need you find the most minimum `r` meet the requirements.

```
# for all k >=2 and k <= ceil(log(n,2))
r >= 0 and r == n - pow(ceil(pow(n,1/k), k)
```

So it's only a iteration. It's easy.

But when you calculate `pow(n, 1/k)` in python, it says the n is too large to converting to a float number.

Emmm.... It seems that python try to pass the calling to some c library, which only support a float/double number.

So we have to calculate this by ourselves. In python, big number is supported. We can use [Newton Method](https://en.wikipedia.org/wiki/Newton%27s_method), which is a classical ` root-finding algorithm`.

like this.

```
def my_find_root(n, k):
x = 2 ** int(math.ceil(math.log(n, 2)/k))
y = ((k-1)*x + n / x ** (k-1)) / k
while y < x:
x = y
y = ((k-1)*x + n / x ** (k-1)) / k
return x
```

So we can make our iteration.

```
def solve(n):
result = None
i = 2
while True:
x = my_find_root(n, i)
r = n - x ** i
if result == None or result >= r:
result = r
if x == 2:
break
i += 1
return result
```

so `solve(n)` is the result.