孤独な素数を見つける


21

孤独な素数(私はそれらを呼ぶ)は素数であり、widthの数グリッドが与えられた場合w ≥ 3、直交または対角線上に他の素数が隣接しない素数です。

たとえば、次の場所でグリッドを取得した場合w = 12(太字で強調されたプライム):

1   2   3   4   5   6   7   8   9   10  11  12
13  14  15  16  17  18  19  20  21  22  23...
 ...86  87  88  89  90  91  92  93  94  95  96
97  98  99  100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120

2つの素数103107のみが直交または対角線上に素数を持たないことがわかります。そこには孤独な素数がないため、セクションをスキップしました。(実際には37を除く)

あなたの仕事は、2つの入力w ≥ 3とを与えられi ≥ 1、幅を持つ数グリッドの最初の孤独な素数を決定するwことiです。ここで、孤独な素数は以上でなければなりません。入力は、合理的な形式で取得できます(文字列としての取得を含む)。幅に対して孤独な素数があることが保証されますw

グリッドは回り込みません。

例:

w  i   output
11 5   11
12 104 107
12 157 157
9  1   151
12 12  37

これは、最短のコードが勝ちます!


なぜ孤独なプライムでw=12はない37のですか?それを取り巻く数字はどれ{25, 26, 38, 49, 50}も素数ではありません。
ジョナサンフレッチ

@JonathanFrechはい、テストケースにはそれが含まれています。
Okx

回答:


8

C(gcc)159 158 149バイト

P(n,d,b){for(d=b=1<n;n>++d;)b*=n%d>0;n=b;}F(w,i){w=P(i)&!(P(i-w)|P(i+w)|i%w>1&(P(~-i)|P(i+~w)|P(i+~-w))|i%w>0&(P(-~i)|P(-~i-w)|P(i-~w)))?i:F(w,++i);}

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


改行をスキップして1バイト節約できます。オンラインでお試しください!
xanoetux

@ceilingcatすばらしい提案、ありがとう。
ジョナサン

5

JavaScript(ES6)、116 104バイト

カリー化構文の入力を受け取ります(w)(i)

w=>g=i=>!(C=(k,n=d=i+k)=>n>0?n%--d?C(k,n):d>1:1)(0)&[i,x=1,i-1].every(j=>C(x-w)&C(w+x--)|j%w<1)?i:g(i+1)

テストケース

コメント済み

w =>                    // main function, taking w
  g = i =>              // g = recursive function, taking i
    !(                  //
      C = (             // define C:
        k,              //   a function taking an offset k
        n = d = i + k   //   and using n and d, initialized to i + k
      ) =>              //
        n > 0 ?         //   if n is strictly positive:
          n % --d ?     //     decrement d; if d does not divide n:
            C(k, n)     //       do a recursive call
          :             //     else:
            d > 1       //       return true if d > 1 (i.e. n is composite)
        :               //   else:
          1             //     return true (n is beyond the top of the grid)
    )(0) &              // !C(0) tests whether i is prime (or equal to 1, but this is safe)
    [                   // we now need to test the adjacent cells:
      i,                //   right side: i MOD w must not be equal to 0
      x = 1,            //   middle    : always tested (1 MOD w is never equal to 0)
      i - 1             //   left side : (i - 1) MOD w must not be equal to 0
    ]                   // for each value j defined above,
    .every(j =>         // and for x = 1, 0 and -1 respectively:
      C(x - w) &        //   test whether i - w + x is composite
      C(w + x--) |      //            and i + w + x is composite
      j % w < 1         //   or j MOD w equals 0, so that the above result is ignored
    ) ?                 // if all tests pass:
      i                 //   return i
    :                   // else:
      g(i + 1)          //   try again with i + 1

2

Python 2、144バイト

f=lambda w,i,p=lambda n:all(n%j for j in range(2,n))*(n>1):i*(any(map(p,~-i%w*(i+~w,i-1,i+w-1)+(i-w,i+w)+i%w*(i-w+1,i+1,i-~w)))<p(i))or f(w,i+1)

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

引数の順序:wi

ここでは外部モジュールは使用されていません。

Python 2 + sympy、127バイト

import sympy
f=lambda w,i,p=sympy.isprime:i*(any(map(p,~-i%w*(i+~w,i-1,i+w-1)+(i-w,i+w)+i%w*(i-w+1,i+1,i-~w)))<p(i))or f(w,i+1)

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

別の投稿には値しません。ここでの唯一の違いsympy.isprimeは、手動で実装されたプライムチェック機能の代わりに使用することです。


2

MATL、38バイト

xx`@1G*:5MeZpt3Y6Z+>3LZ)ft2G<~)X<a~}2M

オンラインでお試しください!または、すべてのテストケースを確認します

説明

コードは基本的に、チャレンジで説明されているように、各反復で1行ずつグリッドを拡大し続けるループで構成されます。

各反復でグリッドを作成した後、最後の行が削除され(それらの素数が孤立しているかどうかはわかりません)、残りの数がテストされて、孤立した素数が少なくとも1つ存在するかどうかが確認されます。これは、2Dコンボリューションを介して行われます。

孤独な素数がある場合、ループを終了し、そのような最初の素数を出力します。それ以外の場合は、次の反復に進み、より大きなグリッドを試行します。

(実際には、コードはグリッドの転置バージョンを使用し、行ではなく列で拡大されます。)

xx        % Take two inputs (implicit): w, i. Delete them. They get copied
          % into clipboard G
`         % Do...while
  @       %   Push iteration index (1-based)
  1G      %   Push w
  *       %   Multiply
  :       %   Range from 1 to that
  5M      %   Push w again (from automatic clipboard M)
  e       %   Reshape into a matrix with w rows in column-major order
  Zp      %   Is prime? Element-wise
  t       %   Duplicate
  3Y6     %   Push neighbour mask: [1 1 1; 1 0 1; 1 1 1]
  Z+      %   2D convolution, maintaining size
  >       %   Greater than? Element-wise. Gives true for lonely primes
  3LZ)    %   Remove the last column
  f       %   Find linear indices of nonzeros
  t       %   Duplicate
  2G      %   Push i
  <~      %   Not less than?
  )       %   Use as logical index: this removes lonle primes less than i
  X<      %   Minimum. This gives either empty or a nonzero value
  a~      %   True if empty, false if nonzero. This is the loop condition.
          %   Thus the loop proceeds if no lonely prime was found
}         % Finally (execute on loop exit)
  2M      %   Push the first found lonely prime again
          % End (implicit). Display (implicit)

1

ジュリア0.6、135バイト

using Primes
f(w,i,p=isprime)=findfirst(j->(a=max(j-1,0);b=min(j+1,w);c=a:b;!any(p,v for v=[c;c+w;c-w]if v>0&&v!=j)&&p(j)&&j>=i),1:w*w)

TIOにはPrimesパッケージがありません。すべての孤独な素数(にfindfirstなるfind)を返すことが許可されている場合、5バイト短くなります。機能を移動しようとするジュリアの試みBaseは、ゴルフを傷つけています(ジュリアの目標ではありません)Primes。0.4に含まれていました。

Ungolfed(主に)

function g(w,i)
    for j=i:w*w
        a,b=max(j-1,0),min(j+1,w)
        c=a:b
        !any(isprime,v for v=[c;c+w;c-w]if v>0&&v!=j)&&isprime(j)&&return j
    end
end

1

ゼリー、20バイト

+‘ÆRœ^ḷ,ḷ’dạ/Ṁ€ṂḊð1#

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

使い方

+‘ÆRœ^ḷ,ḷ’dạ/Ṁ€ṂḊð1#  Main link. Left argument: i. Right argument: w.

                 ð    Combine the links to the left into a chain and begin a new,
                      dyadic chain with arguments i and w.
                  1#  Call the chain to the left with left argument n = i, i+1, ...
                      and right argument w until 1 of them returns a truthy value.
                      Return the match.
+                       Yield n+w.
 ‘                      Increment, yielding n+w+1.
  ÆR                    Yield all primes in [1, ..., n+w+1].
      ḷ                 Left; yield n.
    œ^                  Multiset OR; if n belongs to the prime range, remove it; if
                        it does not, append it.
       ,ḷ               Wrap the resulting array and n into a pair.
         ’              Decrement all involved integers.
          d             Divmod; map each integer k to [k/w, k%w].
           ạ/           Reduce by absolute difference, subtracting [n/w, n%w] from
                        each [k/w, k%w] and taking absolute values.
             Ṁ€         Take the maximum of each resulting pair.
                        A maximum of 0 means that n is not prime.
                        A maximum of 1 means that n has a prime neighbor.
               Ṃ        Take the minimum of the maxima.
                Ḋ       Dequeue; map the minimum m to [2, ..., m].
                        This array is non-empty/truthy iff m > 1.


0

Clean181 ... 145バイト

import StdEnv
@w i=hd[x+y\\y<-[0,w..],x<-[1..w]|x+y>=i&&[x+y]==[a+b\\a<-[y-w,y,y+w]|a>=0,b<-[x-1..x+1]|0<b&&b<w&&all((<)0o(rem)(a+b))[2..a+b-1]]]

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

ゴルフをしていない:

@ w i
    = hd [
        x+y
        \\ y <- [0, w..]
        ,  x <- [1..w]
        | x+y >= i && [x+y] == [
            a+b
            \\ a <- [y-w, y, y+w]
            | a >= 0
            ,  b <- [x-1..x+1]
            | 0 < b && b < w && all ((<) 0 o (rem) (a+b)) [2..a+b-1]
            ]
        ]

0

ゼリー 30  29 バイト

私の推測では、これはおそらくかなりのマージンで打ち負かすことができます

ÆPŒR+€×¥+©⁸’:⁹Ġ®ṁLÞṪFÆPS’¬ð1#

服用進リンクi左にとw孤独な素数を返す右側に。

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

どうやって?

ÆPŒR+€×¥+©⁸’:⁹Ġ®ṁLÞṪFÆPS’¬ð1# - Link: i, w                     e.g. 37, 12
                           1# - find the 1st match starting at i and counting up of...
                          ð   - ...everything to the left as a dyadic link
                              - (n = i+0; i+1; ... on the left and w on the right):
ÆP                            -   is i prime: 1 if so, 0 if not     1
  ŒR                          -   absolute range: [-1,0,1] or [0]   [-1,0,1]
       ¥                      -   last two links as a dyad (w on the right):
      ×                       -     multiply (vectorises)           [-12,0,12]
    +€                        -     add for €ach       [[-13,-1,11],[-12,0,12],[-11,1,13]]
                              -     - i.e. the offsets if including wrapping
          ⁸                   -   chain's left argument, i
        +                     -   add                  [[24,36,48],[25,37,49],[26,38,50]]
                              -     - i.e. the adjacents if including wrapping
         ©                    -   copy to the register
           ’                  -   decrement            [[23,35,47],[24,36,48],[25,37,49]]
             ⁹                -   chain's right argument, w
            :                 -   integer division               [[1,2,3],[2,3,4],[2,3,4]]
              Ġ               -   group indices by value         [[1],[2,3]]
                              -     - for a prime at the right this would  be [[1,2],[3]]
                              -     - for a prime not at an edge it would be [[1,2,3]]
               ®              -   recall from register [[24,36,48],[25,37,49],[26,38,50]]
                ṁ             -   mould like           [[24,36,48],[[25,37,49],[26,38,50]]]
                  Þ           -   sort by:
                 L            -     length             [[24,36,48],[[25,37,49],[26,38,50]]]
                   Ṫ          -   tail                             [[25,37,49],[26,38,50]]
                              -     - i.e the adjacents now excluding wrapping
                    F         -   flatten                          [25,37,49,26,38,50]
                     ÆP       -   is prime? (vectorises)           [0,1,0,0,0,0]
                       S      -   sum                              1
                        ’     -   decrement                        0
                         ¬    -   not                              1            

私の推測では、これはおそらくかなりのマージンで打ち負かされるでしょうか?それはゴルフの言語にとって簡単なことではありません。
エリックアウトゴルファー

いいえ、しかし、それはジェリー(もし外皮でないなら!!)で、私の方法を保存するいくつかの方法、またはより良いアプローチがあるかもしれないという私の推測です
ジョナサンアラン

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