これは連続した素数/定数の指数ですか?


22

少し前に、27000の素因数分解を見ました。

27000 = 2 3 ×3 3 ×5 3

それについて2つの特別なことがあります:

  • 連続素数:素数は連続しています:2は1番目の素数、3は2番目の素数、5は3番目の素数です。
  • constant-exponent:指数はすべての素数で同じです(常に3)

数学的に表現された:

整数xは厳密に正の整数が存在する場合に連続プライム/定数、指数の数であるNKmはその結果、X = P nはM × P N +1 M ×...× P N + k個のMPをjj番目の素数

あなたの仕事は、正の整数がこれらの条件を満たすかどうかをテストすることです。

入力:

妥当な形式の正の整数> 1。

出力:

入力が連続した素数/定数の指数であるかどうかを示す、少なくとも1つは一定でなければならない2つの値の1つ。

エッジケース:

  • 素数pの因数分解はp 1であるため、素数は真実です
  • 以下のように書くことができる他の数のP M pが素数でもtruthyあります。

ルール:

  • 標準の抜け穴が適用されます。
  • 整数オーバーフローの心配はありませんが、255までの数字が機能する必要があります。
  • バイト単位の最短コードが優先されます。

テストケース:

真実:

2
3
4
5
6
7
8
9
11
13
15
27000
456533

偽物:

10
12
14
72
10000000

以下は、いくつかのテストケースを生成するPythonスクリプトです。

私が答えを受け入れたという事実は、挑戦が終わったという意味ではありません。勝者は変わります!


おそらく、このようなすべての番号のリストを生成し、入力がリストにあるかどうかを確認することで、これとは別の方法で来ることができます
Engineer Toast

@EngineerToastしかし、無限に多くの真実の数字があります。
アレクシスオルソン

@AlexisOlsonもちろんですが、多くの言語で整数として処理できる有限です。
エンジニアトースト

あなたの数式はPjがx = Pn^m部品に関連していない。Pnがn番目の素数であることを意味すると思います
ベスカ

@Veskah nには特定の値(xを分割する最初の素数のインデックス)があるため、Pn + 1n + 1番目の素数であることを暗示したい場合Pnn番目の素数であると言うのは厄介です。
デニス

回答:



7

正規表現(のECMAScript)、276の 205 201 193 189バイト

さまざまな素因数の多重度(指数)を比較することは、ECMAScript正規表現で解決するための興味深い問題です。ループの反復を通じて持続する後方参照がないため、何を数えるのも困難です。問題の数値特性を数えることができたとしても、多くの場合、より間接的なアプローチはより良いゴルフになります。

私の他のECMA正規表現の投稿と同様に、ネタバレ警告を出します。ECMAScript正規表現の単項数学問題を解決する方法を学ぶことを強くお勧めします。それは私にとって魅力的な旅であり、自分で試してみたいと思う人、特に数論に興味のある人のためにそれを台無しにしたくありません。1つ1つ解決するための連続したスポイラータグ付きの推奨される問題のリストについては、この以前の投稿参照してください

したがって、高度な単項正規表現の魔法を台無しにしたくない場合は、これ以上読んではいけません。この魔法を自分で理解するためにショットを撮りたい場合は、上記のリンクに記載されているECMAScript正規表現の問題を解決することから始めることを強くお勧めします。

私が以前に開発した正規表現の主なペイロードは、この課題に非常に適用できることが判明しました。それが最高の多重度の素数を見つける正規表現です。私の最初の解決策は非常に長く、後に段階的にゴルフを進め、最初に分子先読みを使用するように書き直し、次に分子先読みの欠如を回避する高度な技術を使用してプレーンECMAScriptに移植し、その後元のプレーンなECMAScriptソリューションよりもはるかに小さくなるように、それをゴルファーします。

この問題に適用される正規表現からの部分は、最初のステップで、すべての素因数を共有するNの最小要素であるQを見つけます。この数を取得したら、Nが「定数指数」であることを示すために必要なことは、それ以上できなくなるまでNをQで除算することです。結果が1の場合、すべての素数の多重度は等しくなります。

Qを見つけるために以前に開発したアルゴリズムを使用して回答を送信した後、まったく異なる方法で計算できることに気づきました:Nの最大の平方フリーファクターを見つける(私のCarmichael数値正規表現と同じアルゴリズムを使用)。結局のところ、これは分子の先読みと可変長の先読みの欠如を回避するという点でまったく困難ではありません*(以前に使用された高度な技術を取り入れる必要はありません)。さらに、平方フリーNと素数Nを異なる特殊なケースとして扱う複雑さを排除し、このソリューションからさらに7バイトを削除します。

(Qを計算するためにここで以前使用された高度な技術を必要とする他の問題がまだ残っていますが、現在それらのどれも私のPPCG投稿によって表されません。)

連続プライムテストの方がずっと遅いため、多重プライムテストを連続プライムテストの前に配置しました。より早く失敗する可能性のあるテストを最初に配置すると、均一に分散された入力の正規表現が高速になります。また、より多くの後方参照を使用するため(2桁の場合はコストが高くなります)、最初に置く方が良いでしょう。

私はで見つかったトリックを使用して(193→189)、この正規表現から4つのバイトを削除することができた汚れた商がより大きい又は除数に等しくなることが保証されている場合には短く分割futherできます。

^(?=(|(x+)\2*(?=\2$))((?=(xx+?)\4*$)(?=(x+)(\5+$))\6(?!\4*$))*x$)(?=.*$\2|((?=((x*)(?=\2\9+$)x)(\8*$))\10)*x$)(?!(((x+)(?=\13+$)(x+))(?!\12+$)(x+))\11*(?=\11$)(?!(\15\14?)?((xx+)\18+|x?)$))

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

# For the purposes of these comments, the input number = N.
^

# Assert that all of N's prime factors are of equal multiplicity
# Step 1: Find Q, the largest square-free factor of N (which will also be the smallest
# factor of N that has all the same prime factors as N) and put it in \2.
# If N is square-free, \2 will be unset.
(?=
    # Search through all factors of N, from largest to smallest, searching for one that
    # satisfies the desired property. The first factor tried will be N itself, for which
    # \2 will be unset.
    (|(x+)\2*(?=\2$))     # for factors < N: \2 = factor of N; tail = \2
    # Assert that tail is square-free (its prime factors all have single multiplicity)
    (
        (?=(xx+?)\4*$)    # \4 = smallest prime factor of tail
        (?=(x+)(\5+$))    # \5 = tail / \4 (implicitly); \6 = tool to make tail = \5
        \6                # tail = \5
        (?!\4*$)          # Assert that tail is no longer divisible by \4, i.e. that that
                          # prime factor was of exactly single multiplicity.
    )*x$
)
# Step 2: Require that either \2 is unset, or that the result of repeatedly
# dividing tail by \2 is 1.
(?=
    .*$\2
|
    (
        # In the following division calculation, we can skip the test for divisibility
        # by \2-1 because it's guaranteed that \2 <= \8. As a result, we did not need to
        # capture \2-1 above, and can use a better-golfed form of the division.
        (?=
            (              # \8 = tail / \2
                (x*)       # \9 = \8-1
                (?=\2\9+$)
                x
            )
            (\8*$)         # \10 = tool to make tail = \8
        )
        \10               # tail = \8
    )*
    x$                    # Require that the end result is 1
)

# Assert that there exists no trio of prime numbers such that N is divisible by the
# smallest and largest prime but not the middle prime.
(?!
    (                          # \11 = a factor of N
        (                      # \12 = a non-factor of N between \11 and \13
            (x+)(?=\13+$)      # \13 = a factor of N smaller than \11
            (x+)               # \14 = tool (with \15) to make tail = \13
        )
        (?!\12+$)
        (x+)                   # \15 = tool to make tail = \12
    )
    \11*(?=\11$)               # tail = \11

    # Assert that \11, \12, and \13 are all prime
    (?!
        (\15\14?)?             # tail = either \11, \12, or \13
        ((xx+)\18+|x?)$
    )
)


* Nが平方フリーであるという特別なケースはなく、分子の先読みでまだきれいです。これにより6バイトがドロップされ、195 187 183バイトのソリューションが得られます。

^(?=(?*(x+))\1*(?=\1$)((?=(xx+?)\3*$)(?=(x+)(\4+$))\5(?!\3*$))*x$)(?=((?=((x*)(?=\1\8+$)x)(\7*$))\9)*x$)(?!(((x+)(?=\12+$)(x+))(?!\11+$)(x+))\10*(?=\10$)(?!(\14\13?)?((xx+)\17+|x?)$))

# For the purposes of these comments, the input number = N.
^

# Assert that all of N's prime factors are of equal multiplicity
# Step 1: Find Q, the largest square-free factor of N (which will also be the smallest
# factor of N that has all the same prime factors as N) and put it in \1.
(?=
    (?*(x+))              # \1 = proposed factor of N
    \1*(?=\1$)            # Assert that \1 is a factor of N; tail = \1
    # Assert that tail is square-free (its prime factors all have single multiplicity)
    (
        (?=(xx+?)\3*$)    # \3 = smallest prime factor of tail
        (?=(x+)(\4+$))    # \4 = tail / \3 (implicitly); \5 = tool to make tail = \4
        \5                # tail = \4
        (?!\3*$)          # Assert that tail is no longer divisible by \3, i.e. that that
                          # prime factor was of exactly single multiplicity.
    )*x$
)
# Step 2: Require that the result of repeatedly dividing tail by \1 is 1.
(?=
    (
        # In the following division calculation, we can skip the test for divisibility
        # by \1-1 because it's guaranteed that \2 <= \8. As a result, we did not need to
        # capture \1-1 above, and can use a better-golfed form of the division.
        (?=
            (             # \7 = tail / \1
                (x*)      # \8 = \7-1
                (?=\1\8+$)
                x
            )
            (\7*$)        # \9 = tool to make tail = \7
        )
        \9                # tail = \7
    )*
    x$                    # Require that the end result is 1
)

# Assert that there exists no trio of prime numbers such that N is divisible by the
# smallest and largest prime but not the middle prime.
(?!
    (                          # \10 = a factor of N
        (                      # \11 = a non-factor of N between \10 and \12
            (x+)(?=\12+$)      # \12 = a factor of N smaller than \10
            (x+)               # \13 = tool (with \14) to make tail = \12
        )
        (?!\11+$)
        (x+)                   # \14 = tool to make tail = \11
    )
    \10*(?=\10$)               # tail = \10

    # Assert that \10, \11, and \12 are all prime
    (?!
        (\14\13?)?             # tail = either \10, \11, or \12
        ((xx+)\17+|x?)$
    )
)

ここでは、可変長の後読みに移植されています。

正規表現(ECMAスクリプト2018)、198 195 194 186 182バイト

^(?=(x+)(?=\1*$)(?<=^x((?<!^\5*)\3(?<=(^\4+)(x+))(?<=^\5*(x+?x)))*))((?=((x*)(?=\1\8+$)x)(\7*$))\9)*x$(?<!(?!(\14\16?)?((xx+)\12+|x?)$)(?<=^\13+)((x+)(?<!^\15+)((x+)(?<=^\17+)(x+))))

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

# For the purposes of these comments, the input number = N.
^

# Assert that all of N's prime factors are of equal multiplicity
# Step 1: Find Q, the largest square-free factor of N (which will also be the smallest
# factor of N that has all the same prime factors as N) and put it in \1.
(?=
    (x+)(?=\1*$)      # \1 = factor of N; head = \1
    (?<=              # This is evaluated right-to-left, so please read bottom to top.
        ^x
        (
            (?<!^\5*)        # Assert that head is no longer divisible by \6, i.e. that
                             # that prime factor was of exactly single multiplicity.
            \3               # head = \4
            (?<=(^\4+)(x+))  # \4 = head / \5 (implicitly); \3 = tool to make head = \4
            (?<=^\5*(x+?x))  # \5 = smallest prime factor of head
        )*
    )
)
# Step 2: Require that the result of repeatedly dividing tail by \1 is 1.
(
    # In the following division calculation, we can skip the test for divisibility
    # by \1-1 because it's guaranteed that \2 <= \8. As a result, we did not need to
    # capture \1-1 above, and can use a better-golfed form of the division.
    (?=
        (             # \7 = tail / \1
            (x*)      # \8 = \7-1
            (?=\1\8+$)
            x
        )
        (\7*$)        # \9 = tool to make tail = \7
    )
    \9                # tail = \7
)*
x$                    # Require that the end result is 1

# Assert that there exists no trio of prime numbers such that N is divisible by the
# smallest and largest prime but not the middle prime.
# This is evaluated right-to-left, so please read bottom to top, but switch back to
# reading top to bottom at the negative lookahead.
(?<!
    # Assert that \13, \15, and \17 are all prime.
    (?!
        (\14\16?)?           # tail = either \13, \15, or \17
        ((xx+)\12+|x?)$
    )

    (?<=^\13+)
    (                        # tail = \13
        (x+)                 # \14 = tool to make tail = \15
        (?<!^\15+)
        (
            (x+)             # \16 = tool (with \14) to make tail = \17
            (?<=^\17+)(x+)   # \17 = a factor of N smaller than \13
        )                    # \15 = a non-factor of N between \13 and \17
    )                        # \13 = a factor of N
)

あなたは置き換えることができ.*$\2\2^
H.PWiz

けれども、私はこれが有効であると考えている:^(?=(|(x+)\2*(?=\2$))(((?=(xx+?)\5*$)(?=(x+)(\6+$))\7(?!\5*$))*x$))(?!(((xx+)(?=\10+$)(x+))(?!\9+$)(x+))\8*(?=\8$)(?!(\12\11?)?(xx+)\14+$))((?=((x*)(?=\2\17+$)x)(\16*$))\19)*\3$
H.PWiz

しかし、最適に近いようには見えません
H.PWiz

6

ゼリー13 6 5バイト

ÆEt0E

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

それでもアウトゴルフ...(-1バイトのErikに感謝)


説明

ÆE     # get a list of prime exponents (noooo long builtin name)
  t0   # remove zeroes on both sides (leading or trailing)
    E  # all remaining elements are equal

œl-> t。trailEの出力に後続ゼロが存在する理由はありません。
エリックアウトゴルファー


@dylnan 2250で失敗します。
デニス

@Dennisおかげで、私はそれが動作しません実現していたが、私はそれが4つのバイトソリューション刺激する期待しています
dylnan

6

JavaScript(ES6)、87バイト

戻り値0 truthyまたはfalsyためのゼロ以外の整数のため。

f=(n,k=2,j,i)=>n%k?j*(P=d=>k%--d?P(d):d==!i)(k)|j-i|(n>1&&f(n,k+1,j||i)):f(n/k,k,j,-~i)

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

コメント済み

f = (                     // f() = recursive function taking:
  n,                      //   n = input
  k = 2,                  //   k = current factor
  j,                      //   j = reference exponent, initially undefined
  i                       //   i = current exponent, undefined each time we start testing
) =>                      //       the next factor
  n % k ?                 // if k is not a divisor of n:
    j * (                 //   ignore the primality of k if j is still undefined
      P = d =>            //     P() = function testing if k is prime:
        k % --d ?         //       decrement d; if d is not a divisor of k:
          P(d)            //         do a recursive call until it is
        :                 //       else:
          d == !i         //         unless i is already defined: d must not be equal to 1
                          //         (if it is: k is the next prime but does not divide n)
    )(k) |                //   initial call to P() with d = k
    j - i | (             //   if both i and j are defined, they must be equal
      n > 1 &&            //   if n is not yet equal to 1,
      f(n, k + 1, j || i) //   go on with k + 1; if j is undefined, set it to i
    )                     //   (otherwise, stop recursion and return what we have)
  :                       // else:
    f(n / k, k, j, -~i)   //   increment the current exponent and go on with n / k

これは、変更によって壊れていたj||ii。現在、多くの誤検知が発生しています。
デッドコード

@Deadcode今のところこれをチェックしたり修正したりできないので、今のところロールバックしました。
アーナウルド

5

CJam30 29バイト

{mFz~)-!\__W=,\0=>\-:mp1#W=&}

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

ほぼ2年(!)年の休憩の後の私の最初の答えなので、おそらくもっとゴルフをすることができます。これは、整数として入力を受け取るブロックです(整数の配列にマッピングすることもできます)。

説明

{        e# Begin block
 mF      e# Factor input, giving an array of primes and their powers
 z~      e# Transpose and dump, giving an array of primes and an array of powers
 )-      e# Check that the powers are the same: subtract each power from the last element
 !       e# Negate to make sure they're all 0
 \__W=,  e# Get the range from 0 to the largest prime minus one
 \0=>    e# Slice that array so it only includes everything larger than the smallest prime
 \-      e# Remove the original primes from the range array
 :mp     e# Check each element for primality. If the input's primes are consecutive,
         e# this will contain no primes
 1#W=    e# Make sure a "1" is not found
 &       e# If the powers are the same AND primes are consecutive, return 1. Otherwise, 0.
}

5

スタックス5 6 バイト

╣♥qJ╬c

実行してデバッグする

開梱されていない、コメントされていない、これはこのように見える。

|n    get the exponents of the prime factorization
0:D   trim leading zeroes
:u    array has exactly a single distinct element

編集: これはで機能しません512。私はそれにいくつかの考えを与え、うまくいけば後で修正します。 今は動作します。


3

スタックス、9 バイト

1は真実、0は偽

αAG<└\{┬⌠

実行してデバッグする

説明

|nX0-u%x:^=      # Full Program, unpacked, implicit input
|n               # Exponents of sequential primes in factorization. (eg. 20 -> [2 0 1])
  X              # Save to X register
   0-            # Remove all '0' from array
     u%          # Get unique numbers and get length of array
       x         # Copy back the array saved to X
        :^       # Is it ascending
         =       # Are the two comparisons equal? implicit output

おそらくもっとゴルフができるかもしれませんが、それは最後の解決策で欠けていたケースをカバーしています。


3

MATL12 11 10バイト

YFtgYsg)Zs

MATL Onlineでお試しください!

remove-leading-zeroesの部分についてLuis Mendoに感謝します。彼はまた、真理値の交換が許可されているため、チャレンジ要件を満たす数値に対しては0を返し、そうでなければ正の値を返すことを指摘しました。

Grosso Modo。これは、順次素因数分解の指数を生成し、先行ゼロを削除して標準偏差を計算します。


0iYFhdzは7バイトで動作すると思います:逐次分解、連続差、非ゼロの数の指数に0を追加します。結果は、1入力が要件を満たしている場合に限ります
ルイスメンドー

@LuisMendo応答が遅れて申し訳ありませんが、別の回答として投稿できます。それは間違いなく非常に異なっています。
Mr. Xcoder

OK、答えとして投稿しました
ルイスメンドー

3

Java 10、223 191 178 176 168バイト

n->{var s=new java.util.HashSet();for(int f=1,i=1,x,j;n>1;){for(x=++i,j=2;j<x;)x=x%j++<1?1:x;if(x>1){for(j=0;n%i<1&&n>(f=0);n/=i)j++;if(f<1)s.add(j);}}return s.size();}

1真実として、偽として返されます>=2

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

説明:

n->{                   // Method with integer parameter and boolean return-type
  var s=new java.util.HashSet();
                       //  Set to keep track of the prime exponents
  for(int f=1,         //  Prime-flag, starting at 1
          i=1,x,j;     //  Index and temp integers
          n>1;){       //  Loop as long as `n` is still larger than 1
    for(x=++i,         //   Set `x` to `i`, after we've increased `i` by 1 first with `++i`
        j=2;           //   Set `j` to 2 (first prime)
        j<x;)          //   Inner loop as long as `j` is still smaller than `x`
      x=x%j++<1?       //    If `x` is divisible by `j`:
         1             //     Set `x` to 1
        :              //    Else:
         x;            //     Leave `x` unchanged
    if(x>1){           //    If `x` is larger than 1 (if `i` is a prime):
      for(j=0;         //     Use `j` as counter, and set it to 0
          n%i<1        //     If `n` is divisible by `i`:
                       //      And loop as long as `n` is still divisible by `i`,
          &&n>         //      and `n` is larger than 0
              (f=0);   //      (and set `f` to 0 at the same time)
          n/=i)        //       Divide `n` by `i`
        j++;           //       And increase `j` by 1
      if(f<1)          //     If the flag `f` is now/still 0:
        s.add(j);}}    //      Add counter `j` to the Set
  return s.size();}    //  Return the amount of items in the Set
                       //  (1 being true, >=2 being false)

入力例:

n=15

  • フラグ1は最初の素数2に残ります(15は2で割り切れないため)。
  • 旗から行く10、すぐに私たちは15以来プライム3にいるよう3で割り切れる、n5(15/3となり1)、およびセットになり[] → [1]
  • 次に、次の素数5をチェックします。5は5で割り切れるため、n1(5/5 1)になり、セットは同じまま[1] → [1]です()。
  • さてn=1、外側のループを停止します。Set([1])に1は、隣接する両方の素数3と5からの1つの項目のみが含まれているため、trueを返します。

n=14

  • フラグから移行1する0(2で割り切れる14ので)最初の素数2。nは7(14/2 1)になり、セットはになり[] → [1]ます。
  • 次に、次の素数3をチェックします。7は3で割り切れないためn、同じままで、Setはになり[1] → [1,0]ます。
  • 次に、次の素数5をチェックします。7も5で割り切れないためn、同じままで、セットも同じまま[1,0] → [1,0]です()。
  • 次に、次の素数7をチェックします。7は7で割り切れるため、n1(7/7 1)になり、セットは同じまま[1,0] → [1,0]です()。
  • さてn=1、外側のループを停止します。Set([1,0])には、1隣接していない素数2と7の要素と0素数3と5の要素の2つの項目が含まれているため、falseを返します。

n=72

  • 72は2で割り切れる(複数回)ため、フラグは最初の素数2 からに1なり0ます。したがって、n9(72/2 3)になり、セットはになり[] → [3]ます。
  • 次に、次の素数3をチェックします。9は3(複数回)で割り切れるため、n1(9/3 2)になり、セットはになり[3] → [3,2]ます。
  • さてn=1、外側のループを停止します。Set([3,2])には、3from prime 2と2from prime 3の2つの項目が含まれているため、falseを返します。

1
<2intを削除して返すことができます(実際には1を返すことを指定します)。
wastl

@wastlああ、2つの値のうち1つだけが一貫しているというルールを逃しました。その場合1、真実であり、2またはそれ以上は偽です。ありがとう。
ケビンCruijssen

賞金をくれた人に感謝しますが、なぜですか?
ケビンクルーッセン

1
ECMAScriptの回答にもっと注意を向けるために「既存の回答に報いる」報奨金を開始しましたが、これは得られた以上の価値があると信じています(報奨金が失敗したと考えています)。週が終わると、賞金を授与するために自分以外の回答を選択するか、デフォルトのままにして最高投票数にしなければなりませんでした。私はそれに値するとは思いませんでしたが、あなたの答えは最高の説明を持っていたので、私はあなたにそれを授与した理由です。良い説明は、PPCGではあまりにもまれです。私の答えについては、時間があるときに計画を立てる必要があります。
デッドコード

1
@Deadcodeああ、だからこそだ。誰かが賞金を始めたのではないかと思ったのですが、偶然に期限が切れてしまい、代わりに私に届きました。まだ私の答えが最高の投票されたものではなく、その理由を少し混乱させました。私はあなたの正規表現のすべての答えに感銘を受けたと言わなければなりません。私はそれらのいくつかを見てきました、そして、私は毎回驚かされます。特に私が後で同じ答えに戻って、あなたがそれをもっとたくさんゴルフしたとき。:DIは、私がこのチャレンジのために1つを見たことがなかったし、それを支持しなかったことに気付いたので、私はちょうどしました。ご存知のとおり、私はあなたの答えに賞金を追加します。:)
ケビン・クルーッセン

2

J、16バイト

-8バイトのFrownyFrogに感謝します!

(=&#+/\=@#])_&q:

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

私の古い解決策:

J、24バイト

[:(1=[:#@~.{.@I.}.])_&q:

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

説明:

_&q: 素数指数

{.@I.}.] 最初の非ゼロ要素を見つけることにより、先行ゼロを削除します。

     }.   drop
       ]  from the list of exponents
{.@       as much items as the first of the 
   I.     indices of non-zero elements

1=[:#@~. 残りのすべての数値が等しいかどうかをテストします。

  [:#@~.  finds the length of the list after removing the duplicates
1=        is it 1?



2

オクターブ、67バイト

@(x)~any(diff(find(h=histc(factor(x),primes(x))))-1)&h(h>0)==max(h)

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

これがヒストグラムを使用する唯一のソリューションだと思います。

説明:

これにより、ヒストグラムが作成されます。ここで、カウントされる変数は入力の因子であり、ビンprimes(x)に配置されます。ビンはすべて入力よりも素数です。次に、素因数の位置を見つけ、各インデックス間の差を取り、1を引きます。ゼロでない要素がある場合(つまり、素数のインデックスの差が1でない場合)、偽の値になります。そうでない場合は、真理値を返します。

次に、ヒストグラムのすべての非ゼロ要素が最大要素に等しいことを確認します。等しくない値がある場合、これは偽の値になり、そうでない場合は、真の値を返します。

これらのブロックが両方とも真実であれば、入力は連続した素定数の指数です!


1

APL(Dyalog Extended)、28バイト

{f p`↓⍭⍵⋄(1=≢∪p)∧∨/f⍷⍸1⍭⍳⍵}

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

どうやって:

{f p`↓⍭⍵⋄(1=≢∪p)∧∨/f⍷⍸1⍭⍳⍵} ⍝ Monadic function, takes an argument ⍵
       ⍭⍵                     ⍝ Prime factors and exponents of ⍵
     `                         split the resulting matrix in 2 vectors
 f p                           assign the factors to f and the powers to p
                               then
                          ⍳⍵    range [1..⍵]
                        1      primality check for each element in the vector
                                where; returns the indices of truthy values
                     f          find the factors; returns a boolean vector
                   ∨/            logical OR reduction
                                logical AND
           (   p)               unique members of the powers
                                tally; returns the number of elements in the vector
            1=                   check if there's only one element





0

APL(NARS)41文字、82バイト

{(1=≢∪+/¨{v=⍵⊃v}¨⍳≢v)∧(1↓w)≡¯1↓1πw←∪v←π⍵}

{π⍵}は、素因数のリスト内の引数ofの関数因数分解です(1つの素数がより長い時間出現する場合は繰り返します)。
{1π⍵}は関数の次の素数です(この場合、引数はスカラーではなく整数の1つの配列であることに注意してください)。テスト:

  h←{(1=≢∪+/¨{v=⍵⊃v}¨⍳≢v)∧(1↓w)≡¯1↓1πw←∪v←π⍵}
  (2..30)/⍨h¨2..30
2 3 4 5 6 7 8 9 11 13 15 16 17 19 23 25 27 29 30 
  h¨27000 456533 72 10000000
1 1 0 0 
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.