Angka positif terkecil yang kekuatan ke-ynya dapat habis dibagi x

15

Tugas

Bilangan bulat yang diberikan xdan yyang keduanya setidaknya 2, temukan angka positif terkecil yang ykekuatan-nya habis dibagi x.

Contoh

Diberikan x=96dan y=2, output harus 24karena kepuasan 24positif terkecil .nn^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 . Solusi dengan kemenangan byte-count terendah.

Referensi

Biarawati Bocor
sumber
Terkait .
Leaky Nun
1
Akan Xselalu lebih besar dariY ?
Fatalkan
@Fatalize Apa hubungannya dengan apa pun?
Leaky Nun
Tidak ada test case di mana X kurang dari Y, dan itu dapat mengurangi panjang beberapa jawaban (setidaknya milikku) jika Xselalu lebih besar dari Y. Saya lebih suka memiliki yang Xbisa lebih besar atau lebih kecil, tetapi kemudian satu test case untuk yang terakhir akan menjadi besar.
Fatalkan
1
Daftar referensi Anda adalah ilustrasi terbaik yang pernah saya lihat tentang kesewenang-wenangan pemesanan tiket OEIS.
Sparr

Jawaban:

7

Brachylog , 19 17 16 15 12 byte

2 byte disimpan berkat @LeakyNun.

:[I:1]*$r=#>

Cobalah online!

Penjelasan

               Input = [X, Y]
:[I:1]*        Get a list [X*I, Y] (I being any integer at this point)
       $r=     Get the first integer which is the Yth root of X*I
          #>   This integer must be strictly positive
               This integer is the Output
Fatalisasi
sumber
1 byte off
Leaky Nun
@ LeakyNun Terima kasih. Ini akan jauh lebih lambat.
Fatalkan
Mengapa ini lebih lambat?
Leaky Nun
4
Mengutip Fatalize yang terkenal: "jangan pedulikan kompleksitas"
Leaky Nun
6

Jelly , 6 byte

ÆE÷ĊÆẸ

Cobalah online! atau verifikasi semua kasus uji .

Bagaimana itu bekerja

ÆE÷ĊÆẸ  Main link. Arguments: x, y

ÆE      Yield the exponents of x's prime factorization.
  ÷     Divide them by y.
   Ċ    Ceil; round the quotients up to the nearest integer.
    ÆẸ  Return the integer with that exponents in its prime factorization.
Dennis
sumber
1
R*%⁸i0juga 6 byte.
Leaky Nun
Saya pikir itu menjamin jawaban yang terpisah.
Dennis
6

JavaScript (ES7), 32 byte

f=(x,y,i=1)=>i**y%x?f(x,y,i+1):i
Neil
sumber
Anda tidak pernah mendefinisikan f. Saya pikir Anda perlu menetapkan fungsi f.
kamoroso94
1
@ kamoroso94 Maaf, saya selamanya melakukan itu.
Neil
5

Jelly , 6 byte

R*%⁸i0

Cobalah online! atau verifikasi semua kasus uji .

Bagaimana itu bekerja

R*%⁸i0  Main link. Arguments: x, y

R       Yield range from 1 to x inclusive.
 *      Raise each to power y.
  %⁸    Take modulo of each with base x.
    i0  Find the 1-based index of the first
        occurence of zero, returns.
Biarawati Bocor
sumber
5

Python 3, 60 43 39 byte

Terima kasih kepada @LeakyNun dan @ Sp3000 untuk bantuannya

f=lambda x,y,i=1:i**y%x<1or-~f(x,y,i+1)

Fungsi yang mengambil input melalui argumen dan mengembalikan output.

Bagaimana itu bekerja

Fungsi ini menggunakan rekursi untuk berulang kali memeriksa bilangan bulat i, dimulai dengan i=1, sampai satu memenuhi kondisi yang diperlukan, di sini i**y%x<1, ditemukan. Ini dicapai dengan mengambil logika orkondisi dan hasil ekspresi untuk i+1incremented, yang ada di sini -~f(x,y,i+1). Ungkapan ini secara terus-menerus mengevaluasi Falsesampai nilai yang memuaskan jditemukan, pada titik mana itu dievaluasi Truedan rekursi berhenti. Karena ini masing-masing setara dengan 0dan 1dalam Python, dan fungsi telah berulang kali menambahkan 1melalui 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

TheBikingViking
sumber
1
def f(x,y,i=1):¶ while i**y%x:i+=1¶ print(i)
Leaky Nun
@ LeakyNun Terima kasih. Saya hanya memikirkan cara yang sedikit lebih pendek untuk melakukannya (43 vs 44) dengan rekursi.
TheBikingViking
2
39:f=lambda x,y,z=1:z**y%x<1or-~f(x,y,z+1)
Sp3000
@ Sp3000 Apakah tidak fungsi Anda kembali Truebukan z?
Leaky Nun
@ LeakyNun Anda kehilangan -~bagian itu, tapi ya itu akan kembali Truejika x1.
Sp3000
4

Haskell, 31 byte

x#y=[n|n<-[1..],mod(n^y)x<1]!!0

Contoh penggunaan: 96#2->24.

Implementasi langsung: coba semua bilangan bulat n, pertahankan yang memenuhi syarat dan pilih yang pertama.

nimi
sumber
2
Juga 31:x#y=until(\n->mod(n^y)x<1)(+1)0
xnor
4

05AB1E (10 byte)

>GN²m¹ÖiNq

Cobalah online

  • > Membaca argumen pertama, menambahkannya, dan mendorongnya di tumpukan
  • Gmuncul stack ( a) dan memulai loop yang berisi sisa program di mana Nmengambil nilai1, 2, ... a - 1.
  • N²mmendorong Ndan 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 pushes a % 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 pushes N on the stack.
  • q terminates the program.

When the program terminates, the top value of the stack is printed.

ruds
sumber
Please post an explanation of how this code works for those nto familiar with your language, but otherwise good job, and nice first post.
Rohan Jhunjhunwala
That link seems interesting.
Leaky Nun
2
Very nice first answer.
Emigna
3

MATL, 9 bytes

y:w^w\&X<

Try it online!

Explanation

y       % Take x and y implicitly. Push x again
        % STACK: x, y, x
:       % Range from 1 to x
        % STACK: x, y, [1, 2, ..., x]
w       % Swap
        % STACK: x, [1, 2, ..., x], y
^       % Power, element-wise
        % STACK: x, [1^y,  2^y, ..., x^y]
w       % Swap
        % STACK: [1^y, 2^y, ..., x^y], x
\       % Modulo, element-wise
        % STACK: [mod(1^y,x), mod(2^y,x), ..., mod(x^y,x)]
        % A 0 at the k-th entry indicates that x^y is divisible by x. The last entry
        % is guaranteed to be 0
&X<     % Arg min: get (1-based) index of the first minimum (the first zero), say n
        % STACK: n
        % Implicitly display
Luis Mendo
sumber
Stack manipulation much.
Leaky Nun
1
Yep. I suspect Jelly will have a big advantage here, since it avoids all those "copy" and "swap"
Luis Mendo
Don't you have find?
Leaky Nun
@LeakyNun Yes, f, but that finds all nonzero indices. So it would have to be ~f1): negatve, find, get the first entry
Luis Mendo
3

Actually, 12 11 bytes

Many thanks to Leaky Nun for his many suggestions. Golfing suggestions welcome. Try it online!

;)R♀ⁿ♀%0@íu

Original 12-byte approach. Try it online!

1WX│1╖╜ⁿ%WX╜

Another 12-byte approach. Try it online!

w┬i)♀/♂K@♀ⁿπ

A 13-byte approach. Try it online!

k╗2`╜iaⁿ%Y`╓N

Ungolfing:

First algorithm

       Implicitly pushes y, then x.
;      Duplicate x.
)      Rotate duplicate x to bottom of the stack.
R      Range [1, x] (inclusive).
♀ⁿ     Map a**y over the range.
♀%     Map a**y%x over the range.
0@í    new_list.index(0)
u      Increment and print implicitly at the end of the program.

Original algorithm

       Implicitly pushes x, then y.
1WX    Pushes a truthy value to be immediately discarded 
         (in future loops, we discard a**y%x)
|      Duplicates entire stack.
         Stack: [y x y x]
1╖     Increment register 0.
╜      Push register 0. Call it a.
ⁿ      Take a to the y-th power.
%      Take a**y mod x.
W      If a**y%x == 0, end loop.
X      Discard the modulus.
╜      Push register 0 as output.

Third algorithm

       Implicitly pushes y, then x.
w      Pushes the full prime factorization of x.
┬      Transposes the factorization (separating primes from exponents)
i      Flatten (into two separate lists of primes and exponents).
)      Rotate primes to the bottom of the stack.
♀/     Map divide over the exponents.
♂K     Map ceil() over all of the divided exponents.
@      Swap primes and modified exponents.
♀ⁿ     Map each prime ** each exponent.
π      Product of that list. Print implicitly at the end of the program.

Fourth algorithm

     Implicitly pushes x, then y.
k╗   Turns stack [x y] into a list [x, y] and saves to register 0.
2    Pushes 2.
  `    Starts function with a.
  ╜i   Pushes register 0 and flattens. Stack: [x y a]
  a    Inverts the stack. Stack: [a y x]
  ⁿ%   Gets a**y%x.
  Y    Logical negate (if a**y is divisible by x, then 1, else 0)
  `    End function.
╓    Push first (2) values where f(x) is truthy, starting with f(0).
N    As f(0) is always truthy, get the second value.
     Print implicitly at the end of the program.
Sherlock9
sumber
@LeakyNun Waiting for one of your winning golf suggestions :D
Sherlock9
@LeakyNun I'd be happy to post those approaches, too, unless you want to post them yourself.
Sherlock9
+1 for the smirk ;)
Leaky Nun
2

R, 61 bytes, 39 bytes, 37 bytes, 34 bytes

I'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.

function(x,y){for(n in 2:x){if(n^y%%x==0){cat(x,y,n);break}}}

Online test can be conducted here: RStudio on rollApp.


Major progress:

function(x,y){which.max((1:x)^y%%x==0)}

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:

function(x,y)which.max((1:x)^y%%x==0)

Finally, it beats out the answer using Python by two bytes. :)

Another progress: (Again!)

function(x,y)which.min((1:x)^y%%x)

Many thanks to Axeman and user5957401 for the help.

Anastasiya-Romanova 秀
sumber
I think that your test link is dead.
TheBikingViking
@TheBikingViking Thanks for pointing that out. I'll edit it after having my late lunch
Anastasiya-Romanova 秀
2
if you use which.min, you could get rid of the ==0. The modulus will return a number, which be no lower than 0.
user5957401
1
@user5957401 Edited.Bolshoe spasibo...
Anastasiya-Romanova 秀
For the same length of 34 bytes you also had the similar function(x,y)which(!(1:x)^y%%x)[1].
plannapus
2

dc, 23 22 bytes

Thanks to Delioth for his tip about input methods, saving a byte

sysxz[zdlylx|0<F]dsFxp

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.

Joe
sumber
1
You technically don't need the ? at the beginning, as a standard way to invoke some things is > echo "x y [program]"|dc, where x and y are the same as the Question- x and y will be dropped onto the stack as normal.
Delioth
@Delioth Interesting, thanks! I always just used the -e option, but I'll use that from now on.
Joe
@Delioth, for me, using quotes throws errors reminding me that " is not implemented in dc, while not using quotes obviously gives shell errors. Is there anything to be done about this? I know stderr can be ignored, but it still bothers me.
Joe
1

05AB1E, 8 bytes

Lsm¹%0k>

Explanation

L         # range(1,x) inclusive
 sm       # each to the power of y
   ¹%     # each mod x
     0k   # find first index of 0 (0-based)
       >  # increment to 1-based

Try it online

Emigna
sumber
1

Perl 6,  26  25 bytes

{first * **$^y%%$^x,1..$x}
{first * **$^y%%$^x,1..*}

Explanation:

# bare block with two placeholder parameters 「$^y」 and 「$^x」
{
  # find the first value
  first

  # where when it 「*」 is taken to the power
  # of the outer blocks first parameter 「$^y」
  * ** $^y
  # is divisible by the outer blocks second parameter 「$^x」
  %% $^x,

  # out of the values from 1 to Inf
  1 .. *
}
Brad Gilbert b2gills
sumber
0

Mathematica, 36 bytes

(i=1;While[n=i++;Mod[n^#2,#]!=0];n)&
martin
sumber
0

Dyalog APL, 11 bytes

Translation of this.

0⍳⍨⊣|(⍳⊣)*⊢

0⍳⍨ find the first zero in
⊣| the division remainders when x divides
(⍳⊣)* the integers one through x, raised to the power of
y

TryAPL online!

Adám
sumber
0

PowerShell v2+, 48 bytes

param($x,$y)(1..$x|?{!(("$_*"*$y+1|iex)%$x)})[0]

Takes input $x and $y. Constructs a range from 1 to $x, then uses Where-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 final 1 at the end, then pipes that to iex (short for Invoke-Expression and similar to eval). 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 be 0, 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 sorted 1..$x, this will be the smallest. That's left on the pipeline and printing is implicit.

Test cases

PS C:\Tools\Scripts\golfing> (26,2),(96,2),(32,3),(64,9),(27,3)|%{($_-join', ')+' -> '+(.\smallest-positive-number-divisor.ps1 $_[0] $_[1])}
26, 2 -> 26
96, 2 -> 24
32, 3 -> 4
64, 9 -> 2
27, 3 -> 3
AdmBorkBork
sumber
0

PHP, 55 33 bytes

$i=1;while($i**$y%$x)$i++;echo$i;
NETCreator Hosting
sumber
0

Perl, 29 26 bytes

Includes +3 for -p (not +1 since the code contains ')

Run with the input on STDIN

power.pl <<< "96 2"

power.pl:

#!/usr/bin/perl -p
/ /;1while++$\**$'%$`}{
Ton Hospel
sumber
0

Pyth, 9 bytes

AQf!%^THG

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

AQf!%^THG  Program. Input: Q
AQ         G=Q[0];H=Q[1]
  f        First truthy input T in [1, 2, 3, ...] with function:
     ^TH    T^H
    %   G   %G
   !        Logical not (0 -> True, all other modulus results -> False)
           Implicitly print
TheBikingViking
sumber
-1

PHP 59 bytes

Sorry, but I can't test this from my mobile. :)

function blahblah($x,$y){
  for($i=0;1;$i++){
    if(!$i^$y%$x){
      return $i;
    }
  }
}

Golfed

function b($x,$y){for($i=0;1;$i++){if(!$i^$y%$x)return $i;}
Roman Gräf
sumber
You're using $z where you should be using $x and I don't think you're incrementing $i in the loop
theLambGoat