Biarkan n
dan b
menjadi bilangan bulat positif lebih besar dari 1
.
Keluarkan jarak dari n
ke kekuatan berikutnya b
.
Untuk n=5
dan b=3
, kekuatan selanjutnya 3
dari 5
adalah9
( 3^2 = 9
), jadi outputnya adalah 9 - 5 = 4
.
Untuk n=8
dan b=2
, kekuatan berikutnya 2
dari 8
adalah 16
( 2^4 = 16
), jadi outputnya adalah 16 - 8 = 8
. Catat itun
ini adalah kekuatan dari 2
contoh ini.
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
Ini adalah kode-golf . Jawaban terpendek dalam byte menang. Celah standar berlaku.
code-golf
arithmetic
Biarawati Bocor
sumber
sumber
;)æċ
!" alih-alih "oww ini sangat susah ..."æċ_⁸
x86-64 Majelis ( Windows x64 Calling Convention ),
1413 bytePendekatan iteratif yang tidak efisien (tapi langsing!) (Dengan kredit ke @Neil untuk inspirasi):
Fungsi di atas mengambil dua parameter bilangan bulat,
n
(lulus dalamECX
register) danb
(lulus dalamEDX
register), dan mengembalikan hasil integer tunggal (dalamEAX
register). Untuk memanggilnya dari C, Anda akan menggunakan prototipe berikut:Ini terbatas pada kisaran integer 32-bit. Itu dapat dengan mudah dimodifikasi untuk mendukung bilangan bulat 64-bit dengan menggunakan register panjang penuh, tetapi akan membutuhkan lebih banyak byte untuk menyandikan instruksi tersebut. :-)
sumber
push 1
+pop rax
hanya dalam 3 byte. Tetapi ... maka Anda tidak perlu melewati penggandaan, sehingga itu masih merupakan penghematan yang masuk akal karena Anda dapat membatalkannyajmp
.C (gcc) ,
3935 bytePerilaku baru yang tidak terdefinisi berkat Erik
Cobalah online!
sumber
f(n,b,i){for(i=b;b<n;b*=i);n=b-n;}
menghemat 5 byte, dan didukung oleh gccb-=n
?b-=n
jika Anda menukar urutanb
dann
?Dyalog APL, 10 byte
2 byte disimpan berkat @ZacharyT
Cobalah online!
Dibawa
n
sebagai argumen kanan danb
sebagai argumen kiri.Menghitung .
b⌊logbn + 1⌋ - n
sumber
⊣-⍨⊢*1+∘⌊⍟⍨
.⊢-⍨⊣*1+∘⌊⍟
for 10 bytes but with swapped arguments so thatn
is the right argument andb
is the left argument. I used ZacharyT's trick of1+∘⌊
to get it down this far.R,
3834 bytesAnonymous function. Stores all values of b to the power of everything in the range [0,n], subtracts n from each, subsets on positive values, and returns the min.
TIO has a non-pryr version, called as
f(n,b)
; this version needs to be called asf(b,n)
.Saved 4 bytes thanks to Jarko Dubbeldam, who then outgolfed me.
Try it online!
sumber
pryr::f({a=b^(0:n)-n;min(a[a>0])})
is a few bytes shorter.pryr::f
when I define a new variable in the function; looks like it works here.sapply(x, sum)
or whatever, that it addssum
to the arguments.Cubix,
2420 bytes-4 bytes thanks to MickyT
Reads in input like
n,b
Fits on a 2x2x2 cube:
Explanation:
I|I0
: read the input, push 0 (counter) to the stack^w
puts the IP to the right place for the loop:Pp-
: computeb^(counter)
, moven
to top of stack, computeb^(counter) - n
?
: turn left if negative, straight if 0, right if positiveO@
: output top of stack (distance) and exit.|?
: proceed as if the top of the stack were zero<;qu;)
: point the IP in the right direction, pop the top of the stack (negative/zero number), moven
to the bottom of the stack, u-turn, pop the top of the stack (b^(counter)
) and increment the counter^w
and the program continues.Watch it online!
Try it online!
sumber
Pwp.I|-.;)^0@O?|uq;<
Haskell, 20 bytes
Try it online!
until
saves the daysumber
05AB1E,
98 bytesTry it online!
Explanation
sumber
ć
instead of¬
.n
implicitly, both in the filter comparison and the absolute difference calculation.Java (OpenJDK 8), 42 bytes
Based on @GovindParmar's C answer.
Try it online!
sumber
MATL,
109 bytesTry it online!
Explanation
Consider inputs
694
and3
as an example.sumber
JavaScript (ES6), 29 bytes
Very similar to Rick's approach but posted with his permission (and some help saving a byte).
Try it
sumber
Mathematica, 24 bytes
thanks Martin
I/O
sumber
1/Log@##
or#2~Log~#
. Or even better swap the order of the inputs and useLog@##
.#^Floor[...]#
is shorter than#^(Floor[...]+1)
. And there's the Unicode operators forFloor
as well.Log@##
! Actually, if you swap the argument order,#^⌊Log@##⌋#-#2&
should be possible for -5 bytes (I think)!C,
4240 bytesThanks to commenter @Steadybox for the tip
sumber
for
instead ofwhile
saves two bytes:o;p(n,b){for(o=b;n>=b;)b*=o;return b-n;}
n/b
instead ofn>=b
R, 30 bytes
Evaluates to the function
Which takes the first power greater or equal than
n
, and then substractsn
from that value.Changed
ceiling(power)
tofloor(power+1)
to ensure that ifn
is a power ofb
, we take the next power.sumber
JavaScript (ES6), 31 bytes
Test cases:
Show code snippet
sumber
n
andb
or justn
), because that saves you from having to passn
recursively.n=>g=(b,p=b)=>p>n?p-n:g(b,p*b)
andn=>b=>(g=p=>p>n?p-n:g(p*b))(b)
.f=(n,i)=>g=(b=i)=>b>n?b-n:g(b*i)
work for 30 bytes? It would need to be called like so:f(324,5)()
. EDIT: Ah, @Neil beat me to it.Octave, 32 bytes
Try it online!
sumber
Octave, 26 bytes
Verify all test cases!
sumber
Ruby, 38 bytes
Two different approaches:
Try it online!
Try it online!
sumber
Haskell, 31 bytes
Try it online!
sumber
Perl 6,
31 3029 bytesTest it (31)
Test it (30)
Test it (29)
Test it (29)
sumber
PARI/GP,
2624 bytessumber
Japt, 9 bytes
Test it online!
Explanation
sumber
Python,
4241 bytesA recursive function which, starting with
v=1
, repeatedly multiplies byb
until it strictly exceedsa
and then returns the difference.Try it online!
Note: The result will never be zero so
a>=v and f(a,b,v*b)or v-a
may be replaced with(a<v)*(v-a)or f(a,b,v*b)
without causing recursion errors.Python 3, 37 bytes?
Using an idea of rici's...
which uses floating point arithmetic (hence results may stray from their true distance),
try that here.
sumber
b-n
never being zero at the same time asn<b
is true).Brachylog, 7 bytes
Try it online!
Takes input as a list
[b, n]
.sumber
PHP>=7.1, 46 bytes
PHP Sandbox Online
sumber
Lua,
7473 ByteA 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 byteExplained
Try it online!
sumber
1
andend
needed?1end
would start to be interpreted as the number1e
then throw an error because1en
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.QBIC, 23 bytes
Takes parameter
b
first, thenn
.Explanation
sumber
Python 2,
4841 bytesFull program without recursion or bit twiddling:
Try it online!
Input format:
n, b
.sumber
Python 3,
5048 bytesThanks to EriktheOutgolfer for saving 2 bytes!
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.
sumber
import math;lambda n,b:b**-~int(math.log(n,b))-n
saves two bytes and is allowed per meta consensus.ceil
would not work.ceil
because it doesn't work for powers ofb
, but as @Uriel pointed out importing before still saves a byte.import
after the lambda, and addf=
in the header.Common Lisp, 73 bytes
Try it online!
sumber