<p>What is the most efficient way to cacluate the closest power of a 2 or 10 to another number? e.g.</p>

<p>3.5 would return 4 for power of 2 and 1 for power of 10</p>

<p>123 would return 128 for power of 2 and 100 for power of 10</p>

<p>0.24 would return 0.25 for power of 2 and 0.1 for power of 10</p>

<p>I'm just looking for the algorithm and don't mind the language.</p>

```
```
n^round(log_n(x))
```
```
where log_n is the logarithm to base n. You may have to modify the round() depending on how you define "closest".
Note that
```
log_n(x)
```
can be implemented as:
```
```
log_n(x) = log(x) / log(n)
```
```
where
```
log
```
is a logarithm to any convenient base.