Tugas
Bilangan bulat yang diberikan x
dan y
yang keduanya setidaknya 2
, temukan angka positif terkecil yang y
kekuatan-nya habis dibagi x
.
Contoh
Diberikan x=96
dan y=2
, output harus 24
karena kepuasan 24
positif terkecil .n
n^2 is divisible by 96
Testcases
x y output
26 2 26
96 2 24
32 3 4
64 9 2
27 3 3
Mencetak gol
Ini adalah kode-golf . Solusi dengan kemenangan byte-count terendah.
Referensi
y=2
: OEIS A019554y=3
: OEIS A019555y=4
: OEIS A053166y=5
: OEIS A015052y=6
: OEIS A015054
X
selalu lebih besar dariY
?X
kurang dariY
, dan itu dapat mengurangi panjang beberapa jawaban (setidaknya milikku) jikaX
selalu lebih besar dariY
. Saya lebih suka memiliki yangX
bisa lebih besar atau lebih kecil, tetapi kemudian satu test case untuk yang terakhir akan menjadi besar.Jawaban:
Brachylog ,
1917161512 byte2 byte disimpan berkat @LeakyNun.
Cobalah online!
Penjelasan
sumber
Jelly , 6 byte
Cobalah online! atau verifikasi semua kasus uji .
Bagaimana itu bekerja
sumber
R*%⁸i0
juga 6 byte.JavaScript (ES7), 32 byte
sumber
f
. Saya pikir Anda perlu menetapkan fungsif
.Jelly , 6 byte
Cobalah online! atau verifikasi semua kasus uji .
Bagaimana itu bekerja
sumber
Python 3,
604339 byteTerima kasih kepada @LeakyNun dan @ Sp3000 untuk bantuannya
Fungsi yang mengambil input melalui argumen dan mengembalikan output.
Bagaimana itu bekerja
Fungsi ini menggunakan rekursi untuk berulang kali memeriksa bilangan bulat
i
, dimulai dengani=1
, sampai satu memenuhi kondisi yang diperlukan, di sinii**y%x<1
, ditemukan. Ini dicapai dengan mengambil logikaor
kondisi dan hasil ekspresi untuki+1
incremented, yang ada di sini-~f(x,y,i+1)
. Ungkapan ini secara terus-menerus mengevaluasiFalse
sampai nilai yang memuaskanj
ditemukan, pada titik mana itu dievaluasiTrue
dan rekursi berhenti. Karena ini masing-masing setara dengan0
dan1
dalam Python, dan fungsi telah berulang kali menambahkan1
melalui bagian yang bertambah, fungsi kembali(j-1)*False + True + (j-1)*1 = (j-1)*0 + 1 + (j-1)*1 = 1 + j-1 = j
, seperti yang diperlukan.Cobalah di Ideone
sumber
def f(x,y,i=1):¶ while i**y%x:i+=1¶ print(i)
f=lambda x,y,z=1:z**y%x<1or-~f(x,y,z+1)
True
bukanz
?-~
bagian itu, tapi ya itu akan kembaliTrue
jikax
1.Haskell, 31 byte
Contoh penggunaan:
96#2
->24
.Implementasi langsung: coba semua bilangan bulat
n
, pertahankan yang memenuhi syarat dan pilih yang pertama.sumber
x#y=until(\n->mod(n^y)x<1)(+1)0
05AB1E (10 byte)
Cobalah online
>
Membaca argumen pertama, menambahkannya, dan mendorongnya di tumpukanG
muncul stack (a
) dan memulai loop yang berisi sisa program di manaN
mengambil nilai1, 2, ... a - 1
.N²m
mendorongN
dan entri kedua dari riwayat input, lalu muncul keduanya dan mendorong yang pertama ke kekuatan yang kedua.¹
pushes the first entry from the input history onto the stack.Ö
pops the previous two stack entries, then pushesa % b == 0
on the stack.i
pops that from the stack. If true, it executes the rest of the program; otherwise, the loop continues.N
pushesN
on the stack.q
terminates the program.When the program terminates, the top value of the stack is printed.
sumber
MATL, 9 bytes
Try it online!
Explanation
sumber
find
?f
, but that finds all nonzero indices. So it would have to be~f1)
: negatve, find, get the first entryActually,
1211 bytesMany thanks to Leaky Nun for his many suggestions. Golfing suggestions welcome. Try it online!
Original 12-byte approach. Try it online!
Another 12-byte approach. Try it online!
A 13-byte approach. Try it online!
Ungolfing:
First algorithm
Original algorithm
Third algorithm
Fourth algorithm
sumber
;)
R,
61 bytes,39 bytes,37 bytes, 34 bytesI'm still a newbie in R programming and it turns out this is my first function I create in R (Yay!) so I believe there's still room for improvement.
Online test can be conducted here: RStudio on rollApp.
Major progress:
which.max
works because it returns the highest value in a vector and if there are multiple it will return the first. In this case, we have a vector of many FALSEs (which are 0s) and a few TRUEs (which are 1s), so it will return the first TRUE.Another progress:
Finally, it beats out the answer using Python by two bytes. :)
Another progress: (Again!)
Many thanks to Axeman and user5957401 for the help.
sumber
which.min
, you could get rid of the==0
. The modulus will return a number, which be no lower than 0.function(x,y)which(!(1:x)^y%%x)[1]
.dc,
2322 bytesThanks to Delioth for his tip about input methods, saving a byte
Uses the stack depth operator
z
for incrementing the test case directly on the stack, and the modular exponentiation operator|
for, well, modular exponentiation. Repeat testing until remainder is not greater than zero.sumber
?
at the beginning, as a standard way to invoke some things is> echo "x y [program]"|dc
, wherex
andy
are the same as the Question- x and y will be dropped onto the stack as normal.-e
option, but I'll use that from now on."
is not implemented indc
, while not using quotes obviously gives shell errors. Is there anything to be done about this? I knowstderr
can be ignored, but it still bothers me.05AB1E, 8 bytes
Explanation
Try it online
sumber
Perl 6,
2625 bytesExplanation:
sumber
Mathematica, 36 bytes
sumber
Dyalog APL, 11 bytes
Translation of this.
0⍳⍨
find the first zero in⊣|
the division remainders when x divides(⍳⊣)*
the integers one through x, raised to the power of⊢
yTryAPL online!
sumber
PowerShell v2+, 48 bytes
Takes input
$x
and$y
. Constructs a range from1
to$x
, then usesWhere-Object
to filter those numbers. The filter takes the string"$_*"
(i.e., the current number with an asterisk) and uses string-multiplication to concatenate those$y
times, then tacks on a final1
at the end, then pipes that toiex
(short forInvoke-Expression
and similar toeval
). This takes the place of[math]::Pow($_,$y)
, since PowerShell doesn't have an exponentiation operator, and is two bytes shorter. That's fed into the modulo operator%
with$x
-- thus, if it's divisible, this will be0
, so we encapsulate that in parens and take the Boolean-not!(...)
thereof. Thus, if its divisible, it'll be included by this filter, and all other numbers will be excluded.Finally, we encapsulate the resultant numbers in parens
(...)
and take the[0]
index. Since the range entered sorted1..$x
, this will be the smallest. That's left on the pipeline and printing is implicit.Test cases
sumber
PHP,
5533 bytessumber
Perl,
2926 bytesIncludes +3 for
-p
(not +1 since the code contains'
)Run with the input on STDIN
power.pl
:sumber
Pyth, 9 bytes
A program that takes input of a list of the form
[x, y]
on STDIN and prints the result.Try it online
How it works
sumber
PHP 59 bytes
Sorry, but I can't test this from my mobile. :)
Golfed
sumber