32
\$\begingroup\$

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.

\$\endgroup\$
0

38 Answers 38

1
2
0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ import math;lambda n,b:b**-~int(math.log(n,b))-n saves two bytes and is allowed per meta consensus. \$\endgroup\$ – Erik the Outgolfer Jun 19 '17 at 14:14
  • \$\begingroup\$ @EriktheOutgolfer ceil would not work. \$\endgroup\$ – Leaky Nun Jun 19 '17 at 14:15
  • \$\begingroup\$ @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. \$\endgroup\$ – notjagan Jun 19 '17 at 14:18
  • \$\begingroup\$ You can reformat it to be completely fine: Try it online!. Just place the import after the lambda, and add f= in the header. \$\endgroup\$ – Mr. Xcoder Jun 19 '17 at 15:27
  • \$\begingroup\$ @Mr.Xcoder Ah, you are correct! I don't know why that didn't occur to me. \$\endgroup\$ – notjagan Jun 19 '17 at 15:30
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

J, 17 bytes

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

Usage

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

Explanation

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

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ – Robert Benson Jun 20 '17 at 15:55
0
\$\begingroup\$

TXR Lisp, 51 bytes

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

Legend: ginterate, op.

\$\endgroup\$
0
\$\begingroup\$

APL(NARS), 13 chars, 26 bytes

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

test:

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

JavaScript (ES6), 49 bytes

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.