プライムの力


16

この課題のために、素数の素数(PPP)は、素数の素数に対する素数として定義できる数として定義されます。例えば、9は3 ^ 2として表現できるため、PPPです。一方、81はPPPではありません。これは、3 ^ 4としてしか表現できず、4が素数ではないためです。最初のいくつかのPPPは次のとおりです:4、8、9、25、27、32、49、121、125、128、169、243、289、343 ...これはOEISシーケンスA053810です

あなたのタスク:

入力整数nに対して、n番目のPPP(1インデックス付きまたは0インデックス付き)のどちらか好きな方を返す/出力するプログラムまたは関数を作成します。

入力:

合理的な方法で受け取った0〜1,000の整数。

出力:

入力によって示されるインデックスのPPP。

テストケース:

これらは1インデックスであるため、プログラムが0インデックスの入力を受け取る場合、指定された入力-1に対して同じ出力に到達する必要があります。

3  -> 9
6  -> 32
9  -> 125

得点:

この、バイト単位の最低スコアが勝ちです!


回答:


8

05AB1E(レガシー) 9  7バイト

@KevinCruijssenに感謝2バイトを保存

µNÓ0Kp»

オンラインでお試しください!

µ           # while counter_variable != input:
 N          #   push iteration counter                       e.g. 125
  Ó         #   get prime exponents                          -> [0, 0, 3]
   0K       #   filter out zeros                             -> [3]
     p      #   is prime?                                    -> [1]
      »     #   join with newlines: we use » instead of J
            #   so that [0,1] is not interpreted as truthy   -> 1
            #   implicit: if 1, increment counter_variable

ああ、»代わりにの使用が好きなJので0\n1、真実だと解釈しないでください!ただし½、これを暗黙的に行うµこの05AB1Eのヒントの 2番目の箇条書き)ので、を省略することで、05AB1Eのレガシーバージョン(TIOでも使用した)にバイトを保存できます。また、ʒĀ}することができます0K7バイト
ケビンクルーッセン

@KevinCruijssenかっこいい。ありがとう!
アーナルド

5

、10バイト

!fȯṗ§*ELpN

オンラインでお試しください!

説明

!fȯṗ§*ELpN  Implicit input.
 f       N  Filter the natural numbers by this function:
  ȯṗ§*ELp    Argument is a number, say 27.
        p    Prime factors: [3,3,3]
       L     Length: 3
      E      Are all elements equal: 1
    §*       Multiply last two: 3
  ȯṗ         Is it prime? Yes, so 27 is kept.
!           Index into remaining numbers with input.


4

Mathematica、48バイト

Sort[Join@@Array[(p=Prime)@#^p@#2&,{#,#}]][[#]]&   

オンラインでお試しください!

しかし、マーティン・エンダーはより良いアイデアを持っていて、6バイトを節約しました

Mathematica、42バイト

Sort[Power@@@Prime@Range@#~Tuples~2][[#]]&   

オンラインでお試しください!


Union代わりにJoinを使用して、を回避できますSort
マーティンエンダー

しかし、私はOuter別のバイトを節約すると思うArray(Union@@Outer[Power,p=Prime@Range@#,p])[[#]]&
マーティン・エンダー

そしてTuplesさらに短い:Sort[Power@@@Prime@Range@#~Tuples~2][[#]]&
マーティン・エンダー



4

ハスケル95 85 80バイト

@Lynnのおかげで-10バイト
-5バイト

0ベース

(!!)[x|x<-[2..],p<-[[i|i<-[2..x],all((>)2.gcd i)[2..i-1]]],or[y^e==x|e<-p,y<-p]]

オンラインでお試しください!

説明

(!!)                    -- point-free expression, partially evaluate index-access operator
[x|x<-[2..]             -- consider all integers x>=2
,p<-                    -- let p be the list of all primes <=x
[[                      -- list of a list so p ends up as a list
i|i<-[2..x],            -- consider all i<=x to be potentially prime
all((>)2.gcd i)[2..i-1] -- if the gcd of i with all smaller integers is
                        -- smaller than 2 then this i is actually prime
 ]],or                  -- if any of the following list entries is true
[y^e==x|                -- the condition y^e==x holds for x with ...
e<-p,y<-p]              -- y and e being prime, i.e. x is a PPP,
]                       -- then add this x to the output sequence / list

f=(!!)[x|x<-[2..],or[y^e==x|y<-p x,e<-p x]]10バイト節約します。
リン

インライン化することで82バイトに到達できます:f=(!!)[x|x<-[2..],p<-[[i|i<-[2..x],all((>)2.gcd i)[2..i-1]]],or[y^e==x|e<-p,y<-p]]。多分、それを数えなくても大丈夫f=ですか?(ルールについてはわからない)。
ネス

私がされたかつて語った確かにすることをf=数えるべきではありません。したがって、80バイトになり(!!)[x|x<-[2..],p<-[[i|i<-[2..x],all((>)2.gcd i)[2..i-1]]],or[y^e==x|e<-p,y<-p]]ます。
ウィルネス

4

パイソン2163の 157 137 136バイト

p=input();r=i=0;e=lambda p:all(p%d for d in range(2,p))
while~-i<p:
 r+=1
 for x in range(r*r):y=x%r;x/=r;i+=x**y==r>e(x)>0<e(y)
print r

オンラインでお試しください!


使用リストの代わりにバイトを保存するには:i=[]及び....i+=[r]*....
フェリペナルディバティスタ


@FelipeNardiBatista最初の反復ではプログラムが関数を定義していたため、リストは使用しませんでした。スポッティングとさらなるゴルフをありがとう。
ジョナサンフレッチ

あなたは返すことができないr代わりにi[p]
ASCIIのみ


2

Pyth、15バイト

e.f/^FR^fP_TSZ2

ここで試してみてください!または、さらにテストケースを確認します。

説明

ef / ^ FR ^ fP_TSZ2-完全なプログラム。Qは入力を意味します。

 .f-真の結果を持つ最初のQ入力。変数Zを使用します。
        fP_TSZ-素数の範囲[1、Z]をフィルターします。
       ^ 2-デカルト正方形。基本的にデカルト積自体。
    ^ FR-べき乗によって各リストを減らします。
  /-Zの出現を^でカウントします。
e-最後の要素。

2

Javascript 137 133バイト

P=n=>{for(p=[i=2];j=++i<n*9;j^i&&p.push(i))
for(;j*j<=i;)j=i%++j?j:i
x=[]
for(i of p)
for(j of p)
x[i**j]=1
return Object.keys(x)[n]}

console.log(P(1000))
console.log(P(800))
console.log(P(9))
console.log(P(5))

**通常のアルゴリズム(結果100ミリ秒)P = n => {

  for(p=[i=2];f=++i<=n*10;!f||p.push(i))
    for(j=0;f&&(x=p[j++])*x<=i;)
      f=i%x
  x=[]
  T=0
  for(i of p)
  for(j of p)
  {
    l= i**j
    if(++T>n &&x.length<l )
    break
    x[l] = 1
  }
  return Object.keys(x)[n]
}

5
うーん、これはcode-golfであり、最速のコードではありません。したがって、他の送信と比較した送信の速度は重要ではありません。これは、バイト数で記録されるためです。回答には提出物のバイト数と言語を含めてください。
グリフォン-モニカの復活

しかし、少なくとも時間制限があるはずです、私はそれをゴルフすることができますが、100msの解決策は5秒の解決策になるでしょう、それは大丈夫ですか?
ダニエルインディー

2
ソリューションの実行には有限の時間がかかる場合があります。唯一の目標は、コードを短くすることです。
グリフォン-モニカの復活

2

APL(Dyalog Extended)、15バイト

{⍵⌷∧∊∘.*⍨¯2⍭⍳⍵}

オンラインでお試しください!

説明

{⍵⌷∧∊∘.*⍨¯2⍭⍳⍵}

                 Right argument. Our input.
{              }  Wraps the function in dfn syntax which allows us to use ⍵.
                  Range [1..⍵].
          ¯2     Get the n-th prime for each n in the range.
      ∘.*⍨        Get the prime powers of each prime.
                 Flatten the list.
                 In Extended, this is monadic sort ascending.
 ⍵⌷               Get the input-th index of the list of prime powers of primes.

2

Perl 6、50バイト

{(sort [X**] (^7028,^24)>>.grep(&is-prime))[$_-1]}

オンラインでお試しください!

  (^7028,^24)            # create 2 ranges from 0
     >>.grep(&is-prime)  # grep for primes in both
 [X**] ...               # calc each exponential pair (2^2, 2^3, 2^5...)
(sort ... )[$_-1]        # sort and get value at index n-1

24と7028の理由は、最大値(n = 1000)が49378729、つまり7027 ^ 2であり、その下に収まる2の最大素数が23であるためです。 23には、最初の1000のすべてのアイテム(および多くのスペア)が含まれています。



1

PARI / GP、48バイト

f(n)=[x|x<-[1..4^n],isprime(isprimepower(x))][n]

f(n)=部品をカウントしない場合、それは43バイトです。


あまり多くの不要なケースをチェックしない、セット表記のない別のアプローチ:

f(n)=c=0;i=1;while(c<n,i++;isprime(isprimepower(i))&&c++);i

0

Java 8、211バイト

import java.util.*;n->{List l=new Stack();for(int a=2,b;a<132;a++)for(b=2;b<132;b++)if(p(a)*p(b)>0)l.add(Math.pow(a,b));Collections.sort(l);return l.get(n);}int p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

非常に非効率的な方法..基本的に2 2から999 999 132 132までのすべてのPPPを計算してリストに保存し、そのリストをソートしてから、nから、そのリストから '番目のアイテムをます。

編集:999 999を使用して28,225アイテムのリストを作成する代わりに、132 132を使用して1,024アイテムのリストを作成します。これにより、パフォーマンスがかなり向上し、チャレンジではインデックス0から1,000までの入力をサポートする必要があると示されているため、完全に受け入れられます。(ただし、変更1e3132てもバイト数には影響しません。)

説明:

ここで試してみてください。

import java.util.*;           // Required import for List, Stack and Collections

n->{                          // Method with integer as parameter and Object as return-type
  List l=new Stack();         //  List to store the PPPs in
  for(int a=2,b;a<132;a++)    //  Loop (1) from 2 to 1,000 (exclusive)
    for(b=2;b<132;b++)        //   Inner loop (2) from 2 to 1,000 (exclusive)
      if(p(a)*p(b)>0)         //    If both `a` and `b` are primes:
        l.add(Math.pow(a,b)); //     Add the power of those two to the List
                              //   End of loop (2) (implicit / single-line body)
                              //  End of loop (1) (implicit / single-line body)
  Collections.sort(l);        //  Sort the filled List
  return l.get(n);            //  Return the `n`'th item of the sorted List of PPPs
}                             // End of method

int p(int n){                 // Separated method with integer as parameter and return-type
  for(int i=2;                //  Index integer (starting at 2)
      i<n;                    //  Loop from 2 to `n` (exclusive)
    n=n%i++<1?                //   If `n` is divisible by `i`:
       0                      //    Change `n` to 0
      :                       //   Else:
       n                      //    Leave `n` the same
  );                          //  End of loop
  return n;                   //  Return `n` (which is now 0 if it wasn't a prime)
}                             // End of separated method

0

J、21バイト

{[:/:~@,[:^/~p:@i.@>:

ゼロインデックスの匿名関数。

オンラインでお試しください!

物事のスイングに戻ろうとしているが、私は良いモナド連鎖を作るためのすべてのトリックを忘れているようだ。

簡単な説明

0番目の素数から入力のインデックスに1を加えた素数までの素数の表を作成します(0を説明するため)。このリストをフラット化してソートし、インデックスを作成します。テーブルが十分に大きくない可能性があるため、これにより一部の値に対して誤った結果が得られる可能性があることがわかりました-その場合、1e4のようなハードコーディングされた値で編集します。私はそれを何らかの方法で証明することはできません(与えられたテストケースに合格します)ので、これが問題かどうかを教えてください。

また21バイト

3 :'y{/:~,^/~p:i.>:y'
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.