いくつかのロンリープライム


10

私は知っています、私は知っています、さらに別の素数の挑戦...

関連する

孤独(または単離された)プライムは素数であるpようにp-2p+2p-4p+4... p-2kp+2kいくつかのためにkすべての複合です。このような素数をth回分離素数と呼びますk

たとえば211、すべて201, 203, 205, 207, 209, 213, 215, 217, 219, 221が複合であるため、5回分離された素数はです。(p-2*5=201p-2*4=203など)

チャレンジ

2つの入力整数、n > 3およびが与えられた場合、厳密により大きいk > 0最小のkth回分離された素数を出力しnます。

たとえば、範囲内のk = 5anyの場合、出力はである必要があります。これは、入力より厳密に大きい最小の5回分離された素数であるためです。n4 ... 210211n

n=55 k=1
67

n=500 k=1
503

n=2100 k=3
2153

n=2153 k=3
2161

n=14000 k=7
14107

n=14000 k=8
14107

ルール

  • 該当する場合は、入力/出力が言語のネイティブ整数型に適合すると想定できます。
  • 入力と出力は、任意の便利な方法で指定できます。
  • 完全なプログラムまたは機能のいずれかが受け入れられます。関数の場合、出力する代わりに出力を返すことができます。
  • 標準的な抜け穴は禁止されています。
  • これはので、通常のゴルフルールがすべて適用され、最短のコード(バイト単位)が勝ちます。

3回分離された素数は2回分離された素数でもありますか?
エリックアウトゴルファー

@EriktheOutgolfer最後の2つのテストケースは確かにそれを確認するようです。
ケビンクルーッセン

1
@KevinCruijssenテストケースは、チャレンジ仕様の一部ではありません。
エリックアウトゴルファー

1
@EriktheOutgolferはい、k定義により、th-times-isolatedはk-1th、k-2thなどです
。– AdmBorkBork

@AdmBorkBork確認したかった、ありがとう。
エリックアウトゴルファー

回答:


3

ゼリー17 13バイト

_æR+⁼ḟ
‘ç1#Ḥ}

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

使い方

‘ç1#Ḥ}  Main link. Left argument: n. Right argument: k

‘       Increment; yield n+1.
    Ḥ}  Unhalve right; yield 2k.
 ç1#    Call the helper link with arguments m = n+1, n+2, ... and k until 1 one
        them returns a truthy value. Return the matching [m].


_æR+⁼ḟ  Helper link. Left argument: m. Right argument: k

_       Subtract; yield m-2k.
   +    Add; yield m+2k.
 æR     Prime range; yield the array of primes in [m-2k, ..., m+2k].
     ḟ  Filterfalse; yield the elements of [m] that do not occur in [k], i.e., [m]
        if m ≠ 2k and [] otherwise.
        The result to the left will be non-empty when m = 2k, as there always is
        a prime in [0, ..., 2m], since m > n > 3.
    ⁼   Test the results to both sides for equality.
        This yields 1 iff m is the only prime in [m-2k, ..., m+2k].

3

、13バイト

ḟ§=;ofṗM+ṡD⁰→

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

説明

とても簡単です。

ḟ§=;ofṗM+ṡD⁰→  Inputs are k and n.
            →  Increment n
ḟ              and find the first number m >= n+1 such that:
         ṡD⁰    Take symmetric range [-2k,..,2k].
       M+       Add m to each.
    ofṗ         Keep those that are prime.
 §=             Check equality with
   ;            the singleton [m].

2

Java 8、144 143バイト

(n,k)->{for(k*=2;;)if(p(++n)>1){int i=-k;for(;i<=k&p(n+i)<2|i==0;i+=2);if(i>k)return n;}}int p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

説明:

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

(n,k)->{                      // Method with two integer parameters and integer return-type
  for(k*=2;                   //  Multiply `k` by 2
      ;)                      //  Loop indefinitely
    if(p(++n)>1){             //   Increase `n` by 1 before every iteration with `++n`
                              //   And if it's a prime:
      int i=-k;for(;i<=k      //    Loop `i` from `-k` to `k` (inclusive)
        &p(n+i)<2|i==0;       //    As long as `n+i` is not a prime (skipping `n` itself)
        i+=2);                //    And iterate in steps of 2 instead of 1
      if(i>k)                 //    If we've reached the end of the loop:
        return n;}}           //     We've found our result, so return it

// Separated method to check if `n` is a prime
// `n` is a prime if it remained unchanged, and not when it became 0 or 1
int p(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}


2

スタックス、14 バイト

åΣ▀ë F▬&■º↔╔^∞

実行してデバッグする

これは、対応するASCII表現です。

w^x:r{Hn+|p_!=m0#

w                   while; run the rest of the program until a falsy value remains
 ^                  increment candidate value.
  x:r               [-x, ..., -1, 0, 1, ... x] where x is the first input
     {        m     map using block, using k from -x to x
      Hn+           double and add to candidate value - this is "p+2k"
         |p         is it prime? produces 0 or 1
           _!       k is zero?
             =      two values are equal; always true for a passing candidate
               0#   any falses left after mapping? if so, continue running

2

JavaScript(Node.js)94 92 89バイト

f=(n,k)=>(Q=y=>y<-k||(P=(a,b=2)=>a>b?a%b&&P(a,b+1):1)(n+2*y)^!!y&&Q(--y))(k,++n)?n:f(n,k)

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

不思議なことに、さらにゴルフをするとスタックオーバーフローが発生します。これだけが14000のサイズで機能します。

最後に、14000でスタックオーバーフローにならない1つのゴルフ。

説明

f=(n,k)=>            // Two inputs
 (Q=y=>              // Function checking whether all numbers in 
                     // [n-2*k, n+2*k] except n are all composite
  y<-k               // The counter runs from k to -k
                     // If none breaks the rule, return true
  ||(P=(a,b=2)=>     // Function checking primality
   a>b?              // Check if a>b
   a%b&&P(a,b+1)     // If a>b and a%b==0 return false, else proceed
   :1                // If a<=b return 1 (prime)
  )(n+2*y)^!!y       // If n+2*y is prime, then y must be 0
                     // If n+2*y is not prime, then y must be non-zero
                     // If none of the conditions are met, return false
  &&Q(--y)           // Else proceed to the next counter
 )
 (k,++n)?            // Add 1 to n first, then start the check
 n                   // If conditions are met, return n
 :f(n,k)             // Else proceed to the next n.


1

Ruby + -rprime73 71 61 57バイト

->n,k{n+=1;(-k..k).all?{|i|(i*2+n).prime?^(i!=0)}?n:redo}

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

学習するのは気持ちがいい!私はよ使用Integer#[]してredo、私はPPCGに、ここで学んだことを技術を。楽しいテクニックの雑草に迷って...

-1バイト:n%2代わりに使用n[0]して、最下位ビットを取得します。ありがとう、Asone Tuhid

-1バイト:ブール式の代わりに三項演算子を使用します。ありがとう、Asone Tuhid

-10バイト:使用XOR演算子が出て入力を避けるために.prime?二回...これはちょうど限りですAsone Tuhid今鉱山などの答え:)

-4バイト:の偶数の値をチェックしても害はありませんnAsone Tuhidはノンストップです。

ゴルフをしていない:

->n,k{
  n += 1;                   # Increment n
  (-k..k).all?{|i|          # In the set [n-2*k, n+2*k], is every number
    (i*2+n).prime? ^ (i!=0) #    EITHER prime XOR different from n itself?
  } ? n                     # If yes, return the current value of n
  : redo                    # Otherwise, restart the block
}


ああ美しい!メタ@ Mr.Xcoderの最新情報をありがとう。
benj2240

1
71バイトn%2短くなってn[0]、この場合と?...:比べて短くすることができる&&...||
Asone Tuhid




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