这是一个连续的质数/常数指数吗?


22

前一段时间,我看了27000的素因数分解:

27000 = 2 3 ×3 3 ×5 3

有两点特别的事情:

  • 连续素数:素数是连续的:2是第一素数,3是第二素数,5是第三素数。
  • 常数指数:每个素数的指数都是相同的(总是3)

用数学表示:

的整数X是一个连续的素数/恒定阶数,如果存在严格的正整数Ñķ中号,使得X = p Ñ × p Ñ +1 ×...× p Ñ + ķ ,其中p j是第j个素数

您的任务是测试正整数是否满足这些条件。

输入:

正整数> 1,采用任何合理形式。

输出:

两个值之一,其中至少一个必须为常数,指示输入是否为连续质数/恒定指数数。

边缘情况:

  • 素数truthy,作为主要的分解pp 1
  • 其他可以写为p m(其中p是素数)的数字也是真实的。

规则:

  • 有标准漏洞。
  • 不用担心整数溢出,但是最大为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 '18年

@Veskah n具有特定值(第一个素数除以x的索引),因此,如果您还想暗示Pn + 1是第n + 1个素数,则说Pn为第n个素数很尴尬。
丹尼斯

Answers:


13

05AB1E,4个字节

Ó0ÛË

在线尝试!

说明

Ó     # get a list of prime exponents
 0Û   # remove leading zeroes
   Ë  # all remaining elements are equal

ÎÓKË除此以外,我能想到的就是其他好东西……我在想,ß但这与我所想的相反。
魔术章鱼缸

7

正则表达式(ECMAScript的),276个 205 201 193 189字节

比较不同主要因子的多重性(指数)是使用ECMAScript正则表达式解决的一个有趣问题-缺少在循环迭代过程中持续存在的反向引用,这对计算任何事物都是一个挑战。即使可以对所讨论的数字特征进行计数,通常也可以采用更间接的方法进行更好的高尔夫运动。

与我在ECMA regex上发表的其他文章一样,我会发出扰人的警告:我强烈建议学习如何在ECMAScript regex中解决一元数学问题。对于我而言,这是一次引人入胜的旅程,对于那些可能想要自己尝试的人,尤其是对数字理论感兴趣的人,我都不想破坏它。请参阅此较早的帖子,以获取一系列连续扰流器标记的建议问题的列表,以逐一解决。

因此,如果您不想破坏一些高级一元正则表达式魔术,请不要再继续阅读。如果您确实想自己弄清楚该魔术,我强烈建议您先解决ECMAScript regex中的一些问题,如上面链接的那部分所述。

我以前开发的正则表达式的主要有效载荷非常适用于此挑战。这是找到最高多重性的质数正则表达式。我的第一个解决方案很长,后来我分阶段进行了深入研究,首先将其重写以使用分子先行,然后使用高级技术将其移植回普通ECMAScript,解决缺少分子先行的问题,随后打高尔夫球,使其比原始的普通ECMAScript解决方案小得多。

正则表达式中适用于此问题的部分是第一步,它找到Q,即共享所有素数的最小N因子。一旦有了这个数字,我们所要做的就是证明N是一个“恒定指数数”,就是将N除以Q,直到不再可用为止。如果结果为1,则所有素数都具有相同的多重性。

在使用我先前开发的算法找到Q并提交答案后,我意识到可以用一种完全不同的方式来计算它:找到N的最大无平方因子(使用与我的Carmichael number regex相同的算法)。事实证明,就避免缺乏分子前行和后向可变长后行(无需引入以前使用的高级技术)而言,这一点都不困难*,并且要短64字节!此外,它消除了将无平方N和素数N视为不同特殊情况的复杂性,从该解决方案中又删除了7个字节。

(仍然存在其他问题,这些问题需要以前在这里使用的先进技术来简化Q的计算,但目前我的PPCG帖子中都没有这些问题。)

我将多重性检验放在连续素数检验之前,因为后者要慢得多。首先进行可能会更快失败的测试,可使正则表达式更快地获得均匀分布的输入。首先使用高尔夫也是更好的选择,因为它使用更多的反向引用(如果使用两位数,则将花费更多)。

我可以使用Grimy发现的技巧从此正则表达式(193→189)中删除4个字节,该技巧可以在商被确保大于或等于除数的情况下进一步缩短除法。

^(?=(|(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?)$
    )
)

在这里它被移植到可变长度的后面:

正则表达式(ECMAScript的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

在线尝试!

仍然令人失望...(感谢Erik提供了-1个字节)


说明

Æ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。ÆE的输出中没有理由出现尾随零。
暴民埃里克(Erik the Outgolfer)'18年


@dylnan失败了2250
丹尼斯

@Dennis谢谢,我已经意识到这行不通,但我希望它能激发一个四字节的解决方案
dylnan

6

JavaScript(ES6),87个字节

对于true,返回0;对于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

@Deadcode我暂时无法检查或更正此问题,因此我现在仅进行了回滚。
Arnauld

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

Stax5 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

Stax,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在线上尝试一下!

感谢Luis Mendo提供了remove-leading-zeroes部分。他还指出,允许交换真值,因此对于满足挑战要求的数字,此值将返回0,否则将返回任何正值。

Grosso Modo,这将生成顺序素数分解的指数,删除前导零并计算标准偏差。


我认为0iYFhdz适用于7个字节:在顺序分解的指数,连续的差,非零数之前加0。结果是1当输入满足要求时
Luis Mendo

@LuisMendo对不起,回复延迟,但您可以将其作为单独的答案发布。这绝对是非常不同的。
Xcoder先生18年

好的,我将其发布为答案
路易斯·门多

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整除)。
  • 当我们到达素数3时,Flag从1变为0。由于15被3整除,因此n变为5(15/3 1),并且Set变为[] → [1]
  • 然后我们检查下一个质数5。由于5被5整除,所以n变成1(5/5 1),并且Set保持不变([1] → [1])。
  • 现在n=1,我们停止外部循环。Set([1])仅包含一项,1来自相邻素数3和5的,因此我们返回true。

n=14

  • 标志从开始10第一个素数2(因为14被2整除)。n变为7(14/2 1),并且Set变为[] → [1]
  • 然后,我们检查下一个质数3。由于7不能被3整除,因此n保持不变,并且Set变为[1] → [1,0]
  • 然后我们检查下一个质数5。由于7也不能被5整除,因此n保持相同,并且Set也保持相同([1,0] → [1,0])。
  • 然后,我们检查下一个素数7。由于7被7整除,所以n变成1(7/7 1),并且Set保持不变([1,0] → [1,0])。
  • 现在n=1,我们停止外部循环。Set([1,0])包含两个项目,1来自非相邻素数2和7的以及0来自素数3和5的,因此我们返回false。

n=72

  • 标志从去10用于所述第一原2,因为72是由2(多次)整除。因此n变为9(72/2 3),并且Set变为[] → [3]
  • 然后,我们检查下一个质数3。由于9被3(可多次除),因此n变为1(9/3 2),并且Set变为[3] → [3,2]
  • 现在n=1,我们停止外部循环。Set([3,2])包含两项,分别是3素数2和2素数3,因此我们返回false。

1
您可以删除<2并返回一个int(指定为true,则返回1)。
wastl

@wastl啊,错过了仅两个值之一保持一致的规则。在那种情况下1是真实的,2或者更高的是假的。谢谢。
凯文·克鲁伊森

感谢谁给了我赏金,但是为什么呢?
Kevin Cruijssen

1
我已经开始了“奖励现有答案”的赏金计划,以使更多的注意力集中在我的ECMAScript答案上,我仍然认为应该得到的奖励比获得的要多(我认为赏金失败了)。当一周结束时,我必须选择一个答案而不是我自己的答案来授予赏金,或者将其默认为投票最高的。我认为没有任何应得的东西,但是您的回答得到了最好的解释,这就是为什么我将它授予您的原因。好的解释在PPCG上太罕见了。至于我的答案,我认为我必须给它一个更好的写作,我打算在有时间的时候计划在上面。
Deadcode

1
@Deadcode啊,这就是原因。我以为可能有人开始了赏金计划,但他们不小心让它过期了,而是降临到了我身上。还是有点困惑为什么我的答案不是最高票数。我必须说我对您的所有Regex答案都印象深刻。我见过其中的一些,每次我都感到惊讶。尤其是当我稍后再回到相同的答案并且您打高尔夫球时,它的负载甚至更大。:DI刚注意到我没有看到也没有对此挑战表示赞同,所以我做到了。您知道的,我会在您的回答中增加一笔赏金。:)
Kevin Cruijssen

2

J,16字节

非常感谢FrownyFrog -8个字节!

(=&#+/\=@#])_&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)

在线尝试!

我相信这是唯一使用直方图的解决方案。

说明:

这将生成一个直方图,其中要计数的变量是输入的因数,并将其放置在bin中primes(x),所有的素数都小于输入。然后,我们找到主要因子的位置,取每个指标之间的差,然后减去一个。如果存在任何不为零的元素(即素数索引的差不为1),则将导致伪造的值,否则将返回真实的值。

然后,我们检查直方图中的所有非零元素都等于最大元素。如果存在不相等的值,则将导致错误的值,否则将返回真实的值。

如果这两个块都是真值,则我们的输入为连续的素数常数指数!


1

APL(Dyalog扩展),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

J,14个字节

1#.2~:/\0,_&q:

输出中的1表示连续的恒定指数。

在线尝试!

        0,_&q:   zero followed by the prime exponents of input
   2~:/\         for every two consecutive values, 1 if they are different
1#.              convert from base-1, just add them up

0

干净,127字节

import StdEnv
@n=[]== $n
?n#j= $n
= @n||j==filter@[hd j..last j]&&any(\p=(prod j)^p==n)[1..n]
$n=[i\\i<-[2..n-1]|n/i*i==n&& @i]

在线尝试!

? :: Int -> Bool使用$ :: Int -> [Int]分解和@ :: Int -> Bool检查素数定义函数。


0

APL(NARS)41个字符,82个字节

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

{π⍵}是素数列表中参数⍵的函数分解(如果一个素数出现更多时间,则重复);
{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 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.