# How far away is n to the next power of b?

Let n and b be positive integers larger than 1.

Output the distance from n to the next power of b.

For n=5 and b=3, the next power of 3 from 5 is 9 (3^2 = 9), so the output is 9 - 5 = 4.

For n=8 and b=2, the next power of 2 from 8 is 16 (2^4 = 16), so the output is 16 - 8 = 8. Note that n is a power of 2 in this example.

Testcases:

n b output
212 2 44
563 5 62
491 5 134
424 3 305
469 8 43
343 7 2058
592 7 1809
289 5 336
694 3 35
324 5 301
2 5 3

This is . Shortest answer in bytes wins. Standard loopholes apply.

# PHP>=7.1, 46 bytes

for([,$n,$b]=$argv;0>=$d=$b**$i++-$n;);echo$d;

PHP Sandbox Online

## Lua, 74 73 Byte

A straight forward solution, I'm using 10 bytes to ensure that the arguments are treated as numbers, and not strings. Outputs to STDIN.

Edit: forgot to remove the space in w=1 n=n+0, saves one byte

n,b=...b=b+0p=b w=1n=n+0while p<=n do p=math.pow(b,w)w=w+1 end print(p-n)

### Explained

Try it online!

n,b=...           -- unpack the argument into the variable n and b
b=b+0             -- set b's type to number
n=n+0             -- set n's type to number
p=b               -- set a variable to track the current value of the powered b
w=1               -- set the nth power
while p<=n        -- iterate untill the current power is greater or equals to n
do
p=math.pow(b,w) -- raise b to the power w
w=w+1           -- increment w
end
print(p-n)        -- outputs p minus the following power of b
• I don't know Lua that well, but is the space between 1 and end needed? Commented Jun 19, 2017 at 14:40
• @ZacharyT In Lua, hexadecimal numbers can be inlined if they start with a number, 1end would start to be interpreted as the number 1e then throw an error because 1en isn't a valid hexadecimal value. This only occure when the letter following the number is [abcdef] as other letters can't be interpreted as hexadecimal value -> w=1while doesn't throw an error. Commented Jun 19, 2017 at 14:44
• Welcome back to PPCG! Commented Jun 19, 2017 at 14:49

# QBIC, 23 bytes

{p=:^q~p>:|_xp-b|\q=q+1

Takes parameter b first, then n.

## Explanation

{       DO
p=:^q   SET p to input b (read as 'a' by QBIC fromt he cmd line) ** q (starts as 1)
~p>:    IF p exceeds 'n' (read as 'b' by QBIC fromt he cmd line)
|_xp-b| THEN QUIT, printing p minus b
\q=q+1  ELSE increase q, re-run

# Python 2, 48 41 bytes

• @Rod's loop simplification saved 7 bytes!

Full program without recursion or bit twiddling:

i=1;n,b=input()
while n>=i:i*=b
print i-n

Try it online!

Input format: n, b.

• You can simplify the loop to reduce 7 bytes
– Rod
Commented Jun 19, 2017 at 15:27
• @Rod Would have never thought of that :). Thanks a lot! Commented Jun 19, 2017 at 15:29

# Python 3, 50 48 bytes

Thanks to EriktheOutgolfer for saving 2 bytes!

lambda n,b:b**-~int(math.log(n,b))-n
import math

Try it online!

Python doesn't have any fancy log or ceiling builtins, so I just went with the obvious approach with a little bit of golfing flair.

• import math;lambda n,b:b**-~int(math.log(n,b))-n saves two bytes and is allowed per meta consensus. Commented Jun 19, 2017 at 14:14
• @EriktheOutgolfer ceil would not work. Commented Jun 19, 2017 at 14:15
• @EriktheOutgolfer I wasn't using ceil because it doesn't work for powers of b, but as @Uriel pointed out importing before still saves a byte. Commented Jun 19, 2017 at 14:18
• You can reformat it to be completely fine: Try it online!. Just place the import after the lambda, and add f= in the header. Commented Jun 19, 2017 at 15:27
• @Mr.Xcoder Ah, you are correct! I don't know why that didn't occur to me. Commented Jun 19, 2017 at 15:30

# Common Lisp, 73 bytes

(defun f(n b)(setq a b)(loop(when(> b n)(return(- b n)))(setq b(* b a))))

Try it online!

# J, 17 bytes

[-~]^[:>.]^.[:>:[

## Usage

f =: [-~]^[:>.]^.[:>:[
424 f 3
305

## Explanation

Performs pow(b, ceil(log(n+1))) where log has base b.

# dc, 23 bytes

snsb1[lb*dln!<F]dsFxln-

I had hoped to be able to use Z to count digits in a given radix, but it seems to count only in decimal. And we might want a base larger than dc supports. So this takes the obvious approach of successively multiplying by b until a value larger than n is reached, at which point we subtract n and leave the result at top of stack.

Annoyingly, we need !< rather than > comparison because the condition excludes an answer of 0.

## Demo

for i in '212 2' '563 5' '491 5' '424 3' '469 8' '343 7'\
'592 7' '289 5' '694 3' '324 5' '2 5' \
'12345678901234567890 1000000'
do printf '%s => ' "$i"; dc -e '?rsnsb1[lb*dln!<F]dsFxln-p' <<<"$i"
done
212 2 => 44
563 5 => 62
491 5 => 134
424 3 => 305
469 8 => 43
343 7 => 2058
592 7 => 1809
289 5 => 336
694 3 => 35
324 5 => 301
2 5 => 3
12345678901234567890 1000000 => 999987654321098765432110

## TXR Lisp, 51 bytes

(defun f(n b)(-[(ginterate(op >= n)(op * b)1)-1]n))

Legend: ginterate, op.

# APL(NARS), 13 chars, 26 bytes

{-⍺-⍵*1+⌊⍵⍟⍺}

test:

f←{-⍺-⍵*1+⌊⍵⍟⍺}
212 f 2
44
563 f 5
62
592 f 7
1809

# JavaScript (ES6), 49 bytes

f=(n,b)=>b**(m=Math).floor(m.log(n)/m.log(b)+1)-n

# Python 3, 48 bytes

lambda n,b:b**int(math.log(n,b)+1)-n;import math

Try it online!

I really wish log was builtin

# Perl 5-pa, 28 bytes

$.*=$F[1]until$.>$_;$_=$.-\$_

Try it online!

# Julia 1.0, 25 bytes

n\b=b^floor(1+log(b,n))-n

Try it online!