35
\$\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

44 Answers 44

1
2
0
\$\begingroup\$

PHP>=7.1, 46 bytes

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

PHP Sandbox Online

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

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
\$\endgroup\$
3
  • \$\begingroup\$ I don't know Lua that well, but is the space between 1 and end needed? \$\endgroup\$
    – Adalynn
    Commented Jun 19, 2017 at 14:40
  • \$\begingroup\$ @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. \$\endgroup\$
    – Katenkyo
    Commented Jun 19, 2017 at 14:44
  • \$\begingroup\$ Welcome back to PPCG! \$\endgroup\$
    – Leaky Nun
    Commented Jun 19, 2017 at 14:49
0
\$\begingroup\$

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ You can simplify the loop to reduce 7 bytes \$\endgroup\$
    – Rod
    Commented Jun 19, 2017 at 15:27
  • \$\begingroup\$ @Rod Would have never thought of that :). Thanks a lot! \$\endgroup\$
    – Mr. Xcoder
    Commented Jun 19, 2017 at 15:29
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\$ Commented Jun 19, 2017 at 14:14
  • \$\begingroup\$ @EriktheOutgolfer ceil would not work. \$\endgroup\$
    – Leaky Nun
    Commented Jun 19, 2017 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
    Commented Jun 19, 2017 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
    Commented Jun 19, 2017 at 15:27
  • \$\begingroup\$ @Mr.Xcoder Ah, you are correct! I don't know why that didn't occur to me. \$\endgroup\$
    – notjagan
    Commented Jun 19, 2017 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\$

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

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

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

Perl 5 -pa, 28 bytes

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

Try it online!

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

Julia 1.0, 25 bytes

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

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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