# 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.

# Japt, 16 bytes

This feels longer than it needs to be!

pÂ(MlV /MlU Ä)-V


Try it

# 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


# AWK, 34+2 bytes

{for(n=$2;n<=$1;n*=$2){}$0=n-$1}1  Try it online! Requires the -M option for arbitrary precision to handle the 12345678901234567890 1000000 => 999987654321098765432110 case. The non-loop version requires the same number of bytes: $0=$2^(int(log($1)/log($2)+1))-$1

• Aahhh... I see that I may not need the 2 bytes for the "large" answer, since I accidentally grabbed Toby Speight's inputs rather than the original inputs. Oh well. This should allow it to work for all cases. – Robert Benson Jun 20 '17 at 15:55

## 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


# Forth (gforth), 45 bytes

: f >r 1 begin i * 2dup < until rdrop - abs ;


Try it online!

### Code Explanation

: f          \ start a new word definition
>r         \ store b on the return stack for quick access
1          \ create a counter to store powers of b
begin      \ start an indefinite loop
i *      \ multiply the counter by b
2 dup <  \ duplicate the counter and n. Check if n is less than the counter
until      \ if it is, end the loop
rdrop      \ remove b from the return stack
- abs      \ subtract and get absolute value (avoid negative)
;            \ end the word definition