繰り返される数字の素数


13

別のシーケンス、別の課題。*

定義

首相はpこのシーケンスである、レッツ・コールもA、すべての桁のIFF dpの小数点以下の拡張、あなたが代わるddのコピーdと結果の整数はまだ素数です。ゼロは許可されません。

たとえば11、このシーケンスでは些細なことです(ちなみに、最初の数字です)。シーケンスの次はある31ので、3331また、素数です。その後53ためには55555333そうでも素数である、と。

チャレンジ

input n、return A(n)、つまりnこのシーケンスのth番目のアイテムを指定します。

始めるための最初の20の用語を以下に示します。これは、OEIS上のA057628です。

11, 31, 53, 131, 149, 223, 283, 311, 313, 331, 397, 463, 641, 691, 937, 941, 1439, 1511, 1741, 1871

これはA(0) = 11A(1) = 31ゼロインデックスを使用する場合、などを意味します。

ルール

  • ゼロベースまたは1ベースのインデックスを選択できます。あなたの答えにどれを指定してください。
  • nth要素だけを返す代わりに、最初のn用語を返すことを選択できます。
  • 入力/出力は、あなたの言語のネイティブ整数フォーマットよりも大きくないと仮定できます。ただし、繰り返し数字の素数言語のネイティブ形式よりも大きい場合があるため、これを考慮する必要があります。
  • たとえば1871、最後の例のには、対応する素数があり18888888877777771、標準のINT32よりもかなり大きくなっています。
  • 完全なプログラムまたは機能のいずれかが受け入れられます。関数の場合、出力する代わりに出力を返すことができます。
  • 出力は、コンソールに出力したり、関数から返されたり、警告ポップアップに表示したりできます。
  • 標準的な抜け穴は禁止されています。
  • これはので、通常のゴルフルールがすべて適用され、最短のコード(バイト単位)が勝ちます。

*公平を期すために、私はシーケンスの最初のいくつかの用語をいくつかの数字で遊んでいたので、OEISに行って残りのシーケンスを取得しました。


2
繰り返される数字の結果もこのシーケンスにあり、繰り返される数字の結果もこのシーケンスにあるなど、無限に素数が存在するのだろうか。ありそうもないようです。
Steadybox

1
@Steadybox 11は、この条件を無限に満たします。しかし、それ以外に、数字の繰り返し操作を適用して素数を取得し続けることができる回数を見るのは興味深いでしょう。
ディルナン

1666666999999999が素数だとすると、なぜ169はシーケンスに含まれないのですか?
パブロオリバ

2
@PabloOlivaそれ169自体は素数ではないため、13 * 13です。
AdmBorkBork

回答:


6

、15バイト

!fo§&öεpd´ṘΠdİp

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

!                 Index into
             İp     the list of primes
 f                    for which:
            d            the digits of p
  o§&                      satisfy both:
     öεpd´Ṙ                  repeated "themselves" times, they form a prime.
           Π                 they are all nonzero.

Erik the Outgolferは1バイト節約しました。の代わりに使用するとεp別のバイトが節約されますが、それによりプログラムが非常に遅くなり、n = 2でも偶数倍になります。


1
H.PWiz I @ ...私たちはここに速度に判断しないと思う
エリックOutgolfer

インタープリターで本当にスピードアップする必要があります。すべての素因数を見つけるよりも遅いのはおかしいです
...-Zgarb

6

05AB1E14 13バイト

エミグナのおかげで-1バイト!

µNSÐPŠ×JpNpPĀ½

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

説明

µNSÐPŠ×JpNpPĀ½
µ              # Do until the input is reached...
 N              # Push the iteration counter
  S             # Split it to its digits
   Ð            # And push two copies of it to the stack
    P           # Get the digital product of the counter
     Š          # And place it two places down the stack
      ×J        # Repeat each digit by itself and join it back to a number
        p       # Check for primality on that result
         Np     # And on the original counter as well
           PĀ   # Create the product and truthify the result
                # Implicit: If it is true increment the input number

5

ゼリー18 14バイト

ÆPaDxDḌÆPaDẠµ#

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

Mr. Xcoder:-1バイト(すべて論理)

Erik the Outgolfer:-2バイト(2行ではなく1行)

HyperNeutrino:-1バイト(シーケンスの最初のn要素を返す)

説明

ÆPaDxDḌÆPaDẠµ#     First Link
ÆP                Is prime?
  a               logical and
   D              convert number to list of digits
    xD            repeat each digit as many times as it's value
      Ḍ           convert to an integer
       ÆP         is prime?
         a        logical and
          D       list of digits
           Ạ      logical all
            µ     the following link as a monad
             #    Do this until n matches are found and return them all

編集:元々、明確に許可されていない10進表現に0を含む数字を含む回答を送信しました。


私は短く独立した答えを作ってみましたが、私は同じことを得ました:(xD
HyperNeutrino


4

アリス72 70 66 62 56バイト

5バイトを節約してくれたLeoに感謝します。

/.\&wh...tz~F0/*$\W.tzt$W?K/ o
\i/&.,a:.$K;d&\FR/K.!w.a%

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

1ベースの入力を使用します。

説明

ここでneatestゴルフトリックは(それが唯一のバイトのカップルを保存していても)私が与える素数判定テスト使用しているということである0複合のためのnのためのn非複合のためのnを。そうすれば、結果を条件で直接使用する必要はありませんが、入力にゼロが含まれていないことを確認する次の部分に直接渡すことができます。

/i\       Read all input in Ordinal mode (the usual way to read decimal input).
&w        Push the current IP position onto the return address stack (RAS)
          n times. This effectively begins our main loop. We will return
          here after each number we've checked, but whenever we come across
          a repeated digit prime (RDP), we will pop one copy of the address
          from the RAS, so that the loops ends once we've found n RDPs.

h.        Increment our main loop iterator X (initially an implicit zero on
          the empty stack) and duplicate it.
.         Make another copy.
.tz       Drop all factors less than X. This gives X for prime X and 1 for
          non-prime X.
~F        Check whether X divides this value. Of course, X divides X so this
          gives X for non-composite X. But X doesn't divide 1 (unless X is 1),
          so we get 0 for composite X. Call this Y.
0         Push a 0.
\         Switch to Ordinal mode.
F         Implicitly convert both to string and check whether Y contains 0.
$/K       If it does, return to the w. Either way, switch back to Cardinal mode.
          Note that the only numbers that get to this point are 1 and prime
          numbers which don't contain 0. It's fine that we let 1 through here,
          because we'll use a proper primality test for the digit-expanded
          version later on.
.!        Store a copy of X on the tape. Let's call the copy that remains on
          the stack Z, which we're now decomposing into digits while expanding
          them.
w         Push the current IP position to the RAS. This marks the beginning
          of an inner loop over the digits of Z.

  .a%       Duplicate Z and retrieve its last digit D by taking Z % 10.
  \./       Duplicate D (in Ordinal mode but that doesn't matter).
  &.        Duplicate D, D times. So we end up with D+1 copies of D.
  ,         Pop the top D and pull up the Dth stack element, which is Z.
  a:        Discard the last digit by taking Z / 10.
  .$K       If Z is zero now, skip the K and end the inner loop, otherwise
            repeat the inner loop.
;         Discard the 0 (what used to be Z).
          We now have D copies of each digit D on the stack, but the digits
          were processed in reverse order, so the last digit is at the bottom.
d&        Repeat the next command once for each stack element.
\*        Concatenate in Ordinal mode. This joins all the digits on the
          stack into a single string.
R         Reverse that string. This is the digit-expanded version of X.
/         Switch back to Cardinal mode.
W         Pop the inner loop's return address from the RAS. We could have done
          this right after the most recent K, but putting it here helps lining
          up the two Ordinal sections in the program layout.
.tzt      Is the digit-expanded number a prime?
$W        If so, we've found an RDP. Pop one copy of the main loop address 
          from the RAS.
g         Recover the current value of X from the top left grid cell.
K         Jump back to the w if any copies of the return address are left 
          on the RAS. Otherwise, we leave the main loop.
/o        Implicitly convert the result to a string and print it in
          Ordinal mode.
          The IP will then bounce off the top right corner and start
          travelling through the program in reverse. Whatever it does
          on the way back is utter nonsense, but it will eventually get
          back to the division (:). The top of the stack will be zero
          at that point and therefore the division terminates the program.

4

Python 2、130バイト

  • この4バイトの短いソリューションを提供してくれたArBoに感謝します。
f=lambda n,c=9:n and f(n-(('0'in`c`)<p(c)*p(int("".join(d*int(d)for d in`c`)))),c+1)or~-c
p=lambda n:all(n%m for m in xrange(2,n))

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


パイソン2195 179 167 140 138 136の 135 134バイト

  • ovsのおかげで27バイト節約されました。のxrange代わりにを使用してrange、aを回避しMemoryError、素数関数を圧縮します。整数インデックスのカウントを改善します。
  • 2バイトを保存しました。バイナリパイプまたは操作|を使用してバイトを節約しますor
  • 2バイトを保存しました。素数関数を反転し、さらにいくつかの論理操作を行います。
  • バイトを保存しました。使用して~-の代わりに、0**ゼロの有無を反転させるjように、&ブール値trueが続くと、この値のブール型プロパティを隔離します。
  • Lynnのおかげで1バイト節約できました。にブール値を指定してゴルフをする~-A&B&C(と同等(not A) and B and C)。A, B, CA<B==C
def f(n,j=9,p=lambda n:all(n%j for j in xrange(2,n))):
 while n:j+=1;n-=("0"in`j`)<p(j)==p(int("".join(d*int(d)for d in`j`)))
 print j

オンラインでお試しください!(1インデックス付き)

説明

f整数インデックス、nおよびデフォルト設定値j、現在のシーケンス候補(9プログラムサイズを維持しながらパフォーマンスを向上させるために開始)、およびプライムチェック関数を受け取るメイン関数を定義 します。ゼロ以外である
限りnn-thシーケンスエントリはまだ見つかりません。したがって、必要な特性を満たす数値である場合、1 jずつ増加し、n減少しjます。
ループが終了するjと、n-番目のシーケンスエントリであるため、出力されます。


私はパーティーに少し遅れていますが、さらに4バイトを
ArBo

@ArBoありがとうございます。
ジョナサンフレッチ

3

Pyth、21バイト

.f&.AKjZT&P_ss*VK`ZP_

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

PythにはDecimal Expansionが組み込まれていないため、かなり長くなります。

  • 最初のN個の正の整数(.f)を取得します。
    • すべての数字が真実である(.AKjZT)、および(&)...
    • 文字列表現と数字(*VK`Z)のベクトル化された乗算、結合されて整数(ss)に変換されたものは素数(P_)、および(&)...
    • それが素数(P_)です。

e新しい規則の修正に従って削除できます。
エリックアウトゴルファー

@EriktheOutgolfer完了、ありがとう
ミスターXcoder

2

Perl 6、51バイト

{(grep {!/0/&is-prime $_&S:g/./{$/x$/}/},2..*)[$_]}

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

  • grep {...}, 2..*中括弧間の述語関数を使用して、2から始まる自然数の無限シーケンスをフィルタリングします。 (...)[$_]関数の引数を使用して、このフィルターされたリストにインデックスを付けます$_
  • !/0/ ゼロ桁を含む数字を除外します。
  • S:g/./{$/ x $/}/ テスト番号の10進数展開の各桁を複製します。
  • is-prime $_ & S:g/./{$/ x $/}/is-primeandジャンクション$_、テスト番号、およびその数字を複製した結果の番号で組み込み関数を呼び出します。and-junctionの両方のメンバーが素数の場合、関数はtrueを返します。

2

J、81バイト

f=.[:1&p:(*@(*/)*x:@#~)&.(10&#.inv)
[:{.(4&p:@{.@]([,]+f@[){:@])^:([>{:@])^:_&2 0

これは、良いJソリューションをまだ見つけていない状況の1つです

それにもかかわらず、私は何か新しいことを学ぶことを期待してこれを投稿します。

f与えられた数が「繰り返し数字の素数」であるかどうかを教えてくれます。次のように分類されます。

[:1&p:                               is the following a prime?
      (*@                            the signum of...
         (*/)                        the product of the digits
             *                       times...
              x:@                    force extended precision of...
                 #~)                 self-duplicated digits
                    &.               "Under": perform this, then perform its inverse at the end
                      (10&#.inv)     convert to a list of digits

そして最後にメインのDo ...動詞でありながら、「現在の素数」と「これまでに見つかった」レジスタの両方を必要とする進捗を保存するためにリストを使用する必要があるという事実から生じる厄介な一見避けられない定型文以来、私たちの左の引数はすでに、すなわち、停止状態を保存するためにとられていますn。これは、引数([および])を指定し、2つの要素リスト({.および{:)を展開するという単純なタスクに多くの貴重なバイトを使用する必要があることを意味します。

[:{.                                                take the first element of the final result, of the following Do... While:
    (4&p:@                                          the next prime after...
          {.@                                       the first element of...
             ]                                      the right arg 
                       {:@])                        the last (2nd) elm of the arg...
              ([,]+f@[)                             those two now become the left and right args to this verb...
               [,                                   left arg appended to...
                 ]+                                 right arg plus...
                   f@[                              f of the left arg...
                             ^:(      )^:_          keep doing all that while...
                                [>                  the left is bigger than...
                                  {:@]              the last elm of the right arg
                                          &2 0      seed the process with 2 0, ie,
                                                    the first prime, and 0 rdps found so far.

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


ヘルパー関数を使用するのは本当に少ないバイトですか?fカッコで囲まれたヘルパー関数に置き換えることはできません。また、ヘルパー機能をゴルフで試してみましたが1 p:('x',~"."0#])&.":、残念ながら「0」が含まれる素数を除外できませんでした。何か考えはありますか?また、'x',~余分な精度を得るために部品が必要です
...-

@coleはい再:私は、なぜ単に明確さを保持し、多分マイルまたはFrownyFrogが本当のバイトを節約するアイデアをしてチャイムになる、気に考え出したので、ヘルパー関数は、バイトが追加されますが、この時点で我々はタイタニック号に真鍮を研磨しています
ジョナ

ヘルパー機能のゴルフを後で確認します
ジョナ

これまで57バイト、範囲の拡張(((0>.-)((*&(1&p:)0&e.|10#.#~),.&.":))([,(+*)~)])/^:_@,&2に使用。10xそうでない場合、n = 15は937をスキップします
マイル

@マイル、あなたはJ神です。すでにここでいくつかの素敵な新しいトリックを見つけました。明日もう一度調べて、繰り返し/減分を理解するようにします。あなたが私のSO質問へのリンクに気づいたかどうかはわかりませんが、これは私がそこに提起した問題を解決するために答えることができる一般的なテクニックであると言いますか?
ジョナ
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.