Pulihkan yang utama dari kekuatan yang utama

13

Definisi : kekuatan prima adalah bilangan alami yang dapat diekspresikan dalam bentuk p n di mana p adalah prima dan n adalah bilangan alami.

Tugas : Diberi kekuatan prima p n > 1, kembalikan prima p.

Testcases :

input output
9     3
16    2
343   7
2687  2687
59049 3

Penilaian : Ini adalah . Jawaban terpendek dalam byte menang.

Biarawati Bocor
sumber
1
Bisakah n1?
user202729
@ user202729: Pada kotak uji ke-4 n = 1.
Emigna
15
Mungkin akan lebih sulit untuk mendapatkan bagian kekuatan daripada bagian utama. Seperti ini, ini hanya "Dapatkan faktor terendah yang bukan 1"
Jo King

Jawaban:

13

Bahasa Pemrograman Shakespeare , 209 207 byte

T.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Listen tothy!Page:You cat!Scene V:.Page:You be the sum ofyou a cat!Be the product ofthe quotient betweenI you you worse I?If soLet usScene V.Open heart

Cobalah online!

(I/you)*you<Ilebih pendek dari I%you>0pada SPL.

pengguna202729
sumber
1
Alat yang tepat untuk pekerjaan itu.
Jeremy Weirich
7

Java 8, 46 39 37 byte

n->{int r=1;for(;n%++r>0;);return r;}

-7 byte secara tidak langsung berkat @Tsathoggua .
-2 byte berkat JoKing

Cobalah online.

Penjelasan:

n->{               // Method with integer as both parameter and return-type
  int r=1;         //  Start the result-integer `r` at 1
  for(;n%++r>0;);  //  Increase `r` by 1 before every iteration with `++r`
                   //  and loop until `n` is divisible by `r`
  return r;}       //  After the loop, return `r` as result
Kevin Cruijssen
sumber
Mengikuti jawaban Luis Mendo dengan python3 , mungkinkah menulis n->{for(int i=1;++i<=n;)if(n%i<1)return i;}untuk mendapatkan 43 karakter? (Saya tidak bisa bahasa Jawa.)
Tsathoggua
@ Tsathoggua Seperti yang Anda miliki sekarang tidak, karena metode Java harus selalu kembali. n->{for(int i=1;++i<=n;)if(n%i<1)return i;return n;}akan bekerja, tetapi sayangnya lebih lama. Java dapat memiliki satu pengembalian dalam loop tak terbatas, yang memang menghemat byte, jadi terima kasih! n->{for(int i=1;;)if(n%++i<1)return i;}. Karena iakan menjadi nakhirnya (seperti dengan kasus uji 2687) dan n%n==0, i<=ntidak diperlukan dalam kasus ini.
Kevin Cruijssen
1
Bagaimana dengan 37 byte . Saya tidak cukup akrab dengan Jawa untuk melihat apakah ada lagi yang bisa bermain golf
Jo King
@JoKing Saya tidak melihat apa pun untuk bermain golf lebih jauh, jadi terima kasih untuk -2.
Kevin Cruijssen
5

Python 3 , 36 35 byte

-1 byte berkat mathmandan

f=lambda n,x=2:n%x and f(n,x+1)or x

Cobalah online!

Fungsi rekursif yang menemukan faktor pertama lebih besar dari 1

Jo King
sumber
1
Bagus. Anda dapat (biasanya) menyimpan byte jika Anda mengganti if/elsedengan and/or. Seperti f=lambda n,x=2:n%x and f(n,x+1)or x,.
mathmandan
4

MATL , 4 3 byte

Yfu

Cobalah online!

Penjelasan:

       % Implicit input:      [59049]
Yf     % Prime factorization: [3 3 3 3 3 3 3 3 3 3]
  u    % Unique elements:     [3]
       % Implicit output
Stewie Griffin
sumber
1
Peningkatan yang sangat bagus! Saya akan mendukung lagi :-)
Luis Mendo
4

Spasi , 80 61 60 byte

[S S T  T   N
_Push_-1][S S S N
_Push_0][T  N
T   T   _Read_STDIN_as_number][N
S S N
_Create_Label_LOOP][S S S T N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][S S S N
_Push_0][T  T   T   _Retrieve][S N
T   _Swap][T    S T T   _Modulo][N
T   T   N
_If_0_Jump_to_Label_LOOP][S S T T   N
_Push_-1][T S S N
_Multiply][T    N
S T _Print_as_number]

-20 byte berkat @ JoKing .

Huruf S(spasi), T(tab), dan N(baris baru) ditambahkan hanya sebagai penyorotan.
[..._some_action]ditambahkan sebagai penjelasan saja.

Cobalah online (dengan spasi, tab, dan baris baru saja).

Penjelasan dalam pseudo-code:

Integer n = STDIN as integer
Integer i = -1
Start LOOP:
  i = i - 1
  if(n modulo-i is negative)
    Go to next iteration of LOOP
  else
    i = i * -1
    Print i
    Exit with error: No exit defined

Contoh dijalankan: input = 9

Command   Explanation                    Stack        Heap     STDIN    STDOUT    STDERR

SSTTN     Push -1                        [-1]
SSSN      Push 0                         [-1,0]
TNTT      Read STDIN as integer          [-1]         {0:9}    9
NSSN      Create Label_LOOP              [-1]         {0:9}
 SSSTN    Push 1                         [-1,1]       {0:9}
 TSST     Subtract top two (-1-1)        [-2]         {0:9}
 SNS      Duplicate top (-2)             [-2,-2]      {0:9}
 SSSN     Push 0                         [-2,-2,0]    {0:9}
 TTT      Retrieve                       [-2,-2,9]    {0:9}
 SNT      Swap top two                   [-2,9,-2]    {0:9}
 TSTT     Modulo top two (9%-2)          [-2,-1]      {0:9}
 NTSN     If neg.: Jump to Label_LOOP    [-2]         {0:9}

 SSTTN    Push -1                        [-2,-1]      {0:9}
 TSST     Subtract top two (-2-1)        [-3]         {0:9}
 SNS      Duplicate top (-2)             [-3,-3]      {0:9}
 SSSN     Push 0                         [-3,-3,0]    {0:9}
 TTT      Retrieve                       [-3,-3,9]    {0:9}
 SNT      Swap top two                   [-3,9,-3]    {0:9}
 TSTT     Modulo top two (9%-3)          [-3,0]       {0:9}
 NTSN     If neg.: Jump to Label_LOOP    [-3]         {0:9}
 SSTTN    Push -1                        [-3,-1]      {0:9}
 TSSN     Multiply top two (-3*-1)       [3]          {0:9}
 TNST     Print as integer               []           {0:9}             3
                                                                                  error

Program berhenti dengan kesalahan: Tidak ditemukan jalan keluar.

Kevin Cruijssen
sumber
1
Do you need the i == n check? n%n would be 0 anyway
Jo King
@JoKing Ah, of course. Thanks, 19 bytes saved right there. :)
Kevin Cruijssen
Could you only loop if not n%i and call the print afterwards?
Jo King
1
@JoKing saya cukup yakin tidak. Whitespace tidak benar-benar memiliki loop, hanya memiliki lompatan ke label. Tiga opsi yang saya miliki adalah: 1. melompat ke label tertentu tanpa syarat; 2. lompat ke label tertentu jika bagian atas tumpukan adalah 0; 3. lompat ke label tertentu jika bagian atas tumpukan negatif. Sayangnya tidak ada "lompat ke label jika positif" untuk melanjutkan loop. Saya bisa menyelesaikan ini dengan mengalikan -1 sebelum memeriksa negatif, tapi saya ragu itu akan lebih pendek.
Kevin Cruijssen
1
Tried to do it with a negative modulus and ended up at <s>62</s>60 bytes (yay). Turns out you can't store at negative heap addresses (though 0 saved a couple of bytes)
Jo King
4

Octave, 16 bytes

@(x)factor(x)(1)

Try it online!

Explanation:

@(x)              % Anonymous function taking x as input
    factor(x)     % Prime factorization
             (1)  % Get the first element

Or:

@(x)max(factor(x))  % the makeup of makeup artists
Stewie Griffin
sumber
1
+1 for max factor
Brain Guider
3

Funky, 30 bytes

n=>fori=2n>i i++if1>n%i breaki

Try it online!

ATaco
sumber
0== can be 1> I think.
Kevin Cruijssen
2

Jelly, 3 bytes

ÆfḢ

Try it online!

ÆfṪ, ÆfX could also be seriously competing functions.
ÆfQ could be a seriously competing full program.

Erik the Outgolfer
sumber
2

Forth (gforth), 34 bytes

: f 1 begin 1+ 2dup mod 0= until ;

Try it online!

Explanation

  1. Iterate integers starting from 2
  2. Stop and return when you find one that divides n with no remainder

Code Explanation

: f               \ Define a new word
  1               \ place a 1 on the stack (to use as a counter/index)
  begin           \ start indefinite loop
    1+ 2dup       \ increment counter and duplicate counter and prime power
    mod           \ calculate power % index
  0= until        \ end the loop if modulus is 0 (no remainder)
;                 \ end word definition
reffu
sumber
1

Brachylog, 2 bytes

ḋh

Try it online!

Explanation

ḋ       Prime decomposition
 h      Head
Fatalize
sumber
1

Neim, 1 byte

𝐔

Try it online!

Okx
sumber
U+1D414 is one character, but in UTF-8 and UTF-16 this is represented by 4 bytes.
Ruud Helderman
1
@RuudHelderman Correct, but this isn't in UTF-8 nor UTF-16.
Okx
1
@RuudHelderman You may want to see Neim codepage.
JungHwan Min
@JungHwanMin Thanks; browsing Okx's earlier Neim submissions, I noticed my slightly ignorant reaction wasn't the first. Clever feature, but far from obvious; warrants explanation (as done here). Quoting code-golf tag info: "Unless the question is specified to be scored by characters, it is scored by bytes. If it doesn't specify a character encoding to use for scoring, answers which use Unicode code points outside 0 to 255 should state the encoding used."
Ruud Helderman
@RuudHelderman per meta consensus, if an answer does not specify an encoding, it defaults to the language's default encoding. If that doesn't exist, then it is UTF-8. In this case, Neim has a defined default encoding, so it is assumed to be the encoding of the answer, without the answerer having to explain as such.
JungHwan Min
1

Mathematica, 17 bytes

Divisors[#][[2]]&

The second smallest divisor.

for Monica
sumber
1

R, 32 26 bytes

@Giuseppe with different logic and a shorter solution:

(x=2:(n=scan()))[!n%%x][1]

Try it online!

Original:

numbers::primeFactors(scan())[1]

Try it online!

This is obviously a much superior port of the 05AB1E solution.

ngm
sumber
0

PowerShell, 31 bytes

param($a)(2..$a|?{!($a%$_)})[0]

Try it online!

Constructs a range from 2 to input $a, pulls out those elements where (?) the modulo operation % results in a zero !(...) (i.e., those that are divisors of $a), and then takes the smallest [0] one thereof. That's left on the pipeline, output is implicit.

AdmBorkBork
sumber
0

Perl 6, 22 bytes

{grep($_%%*,2..$_)[0]}

Try it online!

Anonymous code block that filters the factors of the range of 2 to the input and returns the first one. I tried using ^$ to save 2 bytes, but that didn't work in the case that the input was prime.

Jo King
sumber
0

Visual Basic .NET (.NET Framework v4.5), 123 71 bytes

-52 bytes thanks to @Jo King

Function A(n)
For i=n To 2 Step-1
A=If(n Mod i=0,i,A)
Next
End Function

Try it online!

Ungolfed:

Function A(input As Long) As Long
    For i = input To 2 Step -1
        A = If (input Mod i = 0, i, A)
    Next
End Function

Explanation:

The i loop searches backwards from the first number, and finds all numbers that divide it evenly. Because we are going backwards, the smallest is stored in the vairable A.

VB gives you a free variable that matches your function name (in my case, A). At the end of the function execution, the value in that variable is returned (barring an explicit Return statement.

Brian J
sumber
1
You don't need the prime check at all. The smallest factor of a number (other than 1) is guaranteed to be a prime, otherwise there would be a smaller factor
Jo King
@JoKing D'oh! Of course, can't believe I missed that. Thanks!
Brian J
0

Ruby, 100 bytes

require"prime"
i=gets.to_i
Prime.each(i){|p|(1..i).each{|n|c=p**n==i
    puts p if c
    exit if c}}

Try it online!

Alex Allen
sumber