我的电话号码是de Polignac号码吗?


21

当且仅当它是奇数并且不能p + 2 n的形式表示时,该数字才是de Polignac数,其中n是非负整数,p是质数。

任务

编写一些采用正整数并确定它是否为de Polignac数的代码。您可以输出两个不同的值,一个为true,一个为false。您应该努力减少字节数。

测试用例

对于积极的情况,这里是OEIS

1, 127, 149, 251, 331, 337, 373, 509, 599, 701, 757, 809, 877, 905, 907, 959, 977, 997, 1019, 1087, 1199, 1207, 1211, 1243, 1259, 1271, 1477, 1529, 1541, 1549, 1589, 1597, 1619, 1649, 1657, 1719, 1759, 1777, 1783, 1807, 1829, 1859, 1867, 1927, 1969, 1973, ...

以下是一些负面情况:

22, 57

我们可以有真实和虚假的输出,而不是两个截然不同的输出吗?
Okx

@Okx我要说不。
小麦巫师


错误...对于否定情况,OEIS中基本上没有数字吗?确保我没有错过任何明显的事情。
魔术章鱼缸

@MagicOctopusUrn是的。
小麦巫师

Answers:


11

Japt9 14 13字节

o!²mnU dj |Uv

在线测试!查找1000以下的所有de Polignac整数

1伪造的输入和0真实的输出。

说明

 o!²  mnU dj |Uv
Uo!p2 mnU dj |Uv  : Ungolfed
                  : Implicit: U = input integer (e.g. 9)
Uo                : Create the range [0..U), and map each item X to
  !p2             :   2 ** X.               [1, 2, 4, 8, 16, 32, 64, 128, 256]
      m           : Map each of these powers of 2 by
       nU         :   subtracting from U.   [8, 7, 5, 1, -7, -23, -57, -119, -247]
          d       : Return whether any item in the result is
           j      :   prime.                (5 and 7 are, so `true`)
             |    : Take the bitwise OR of this and
              Uv  :   U is divisble by (missing argument = 2).
                  : This gives 1 if U cannot be represented as p + 2^n or if U is even.
                  : Implicit: output result of last expression

这似乎为2和3提供了错误的结果;它正在返回,false但不是de Polignac号码。
毛茸茸的

@Shaggy 3是固定的,但一开始我们甚至不必处理任何情况。定影。
ETHproductions '17

@Shaggy现在固定。
ETHproductions

我要说的是,修复该问题3没有花费任何字节是一件好事,然后我看到了修复问题2-糟糕!
毛茸茸的

:O +1看起来像编码错误的竞争程序
Downgoat

8

果冻11 10字节

@Dennis节省了1个字节

Ḷ2*³_ÆPS<Ḃ

在线尝试!

怎么运行的

Ḷ2*³_ÆPS<Ḃ   Main link. Argument: n (integer)
Ḷ            Lowered range; yield [0, 1, 2, ..., n-1].
 2*          Reversed exponentiation with 2; yield [1, 2, 4, ..., 2**(n-1)].
   ³_        Reversed subtraction with the input; yield [n-1, n-2, n-4, ..., n-2**(n-1)].
     ÆP      Replace each item with 1 if it is prime, 0 otherwise.
       S     Sum; yield a positive integer if any item was prime, 0 otherwise.
         Ḃ   Yield n % 2.
        <    Yield 1 if the sum is less than n % 2, 0 otherwise.
             This yields 1 if and only if the sum is 0 and n is odd.

Ḷ2*⁸_ÆPS<Ḃ 保存一个字节。tio.run/##ASQA2/9qZWxsef//4bi2Mirigbhfw4ZQUzzhuIL/…–
丹尼斯

@Dennis谢谢,我知道必须有3字节的替代字¬;ḂẠS<Ḃ但是,至少对我来说是这样:-)
ETHproductions '17

8

JavaScript(ES6), 56 54  53字节

返回01

f=(n,p=1,x=y=n-p)=>n>p?y%--x?f(n,p,x):x!=1&f(n,p*2):n

在线尝试!

怎么样?

我们从p=1开始。我们测试y=np是否为合成,并相应地产生布尔值。下一个测试以p×2

一旦p大于n,我们便停止递归并返回n

所有迭代的结果都进行“与”运算,将布尔值强制为01

假设所有中间结果都是真实的,我们将进行如下逐位检验:

1 & 1 & 1 & n

当且仅当n为奇数时才给出1,这是验证输入为de Polignac数所需的最后条件。n


3
很棒的技术。可能是唯一没有明确说出n%2或类似的有效答案:P
ETHproductions '17

这对于2 ^ M + 1形式的de Polignac数具有假阴性,例如262145和2097153(后续的是4722366482869645213697、38668526227668668590590597633,5192296858534827628530496329220097等)。并不是数字的大问题,因为它可以正确识别262139、262259、2097131和2097187。当然,由于递归的原因,我不得不将堆栈大小扩大到非常大的值才能进行测试,并且只测试了上面列出的前两个2 ^ M + 1 de Polignac数附近的范围。
Deadcode

1
@Deadcode感谢您举报。现在已修复。
Arnauld

1
@Arnauld啊,您是对的:)可以肯定的是,我做到了,而且确实,它已修复。
Deadcode

1
@Deadcode整洁!:)
Arnauld

7

Python 2中60个 57 56字节

f=lambda n,k=1,p=-1:k/n or(n-k&n-k-p%k>0)&n&f(n,k+1,p*k)

在线尝试!


哇,这真是低效。通过威尔逊定理进行素数检验。从好的方面来说,它可以在262145和2097153上正常工作(假定无限制的堆栈和bignum大小);其他一些则没有。它的is-prime算法产生4的“真实”值,因为(-6)%4 = 2,但这最终不是问题,因为偶数被拒绝&n&。如果数字5是de Polignac数,那么它会是一个假负数,因为1 + 4 = 5,但这不是问题,因为无论如何2 + 3 = 5。
Deadcode

7

果冻,10 字节

另一种10字节的Jelly提交内容已发布。

_ÆRBS€’×ḂẠ

单点链接,返回1表示de Polignac数,否则返回0

在线尝试!或看到 1000岁以下的孩子

怎么样?

_ÆRBS€’×ḂẠ - Link: number, n  e.g.  1    3      5                  6                   127
 ÆR        - prime range            []   [2]    [2,3,5]            [2,3,5]             [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127]
_          - subtract from n        []   [1]    [3,2,0]            [4,3,1]             [125,124,122,120,116,114,110,108,104,98,96,90,86,84,80,74,68,66,60,56,54,48,44,38,30,26,24,20,18,14,0]
   B       - convert to binary      []   [[1]]  [[1,1],[1,0],[0]]  [[1,0,0],[1,1],[1]  [[1,1,1,1,1,0,1],[1,1,1,1,1,0,0],[1,1,1,1,0,1,0],[1,1,1,1,0,0,0],[1,1,1,0,1,0,0],[1,1,1,0,0,1,0],[1,1,0,1,1,1,0],[1,1,0,1,1,0,0],[1,1,0,1,0,0,0],[1,1,0,0,0,1,0],[1,1,0,0,0,0,0],[1,0,1,1,0,1,0],[1,0,1,0,1,1,0],[1,0,1,0,1,0,0],[1,0,1,0,0,0,0],[1,0,0,1,0,1,0],[1,0,0,0,1,0,0],[1,0,0,0,0,1,0],[1,1,1,1,0,0],[1,1,1,0,0,0],[1,1,0,1,1,0],[1,1,0,0,0,0],[1,0,1,1,0,0],[1,0,0,1,1,0],[1,1,1,1,0],[1,1,0,1,0],[1,1,0,0,0],[1,0,1,0,0],[1,0,0,1,0],[1,1,1,0],0]
    S€     - sum €ach               []   [1]    [2,1,0]            [1,2,1]             [6,5,5,4,4,4,5,4,3,3,2,4,4,3,2,3,2,2,4,3,4,2,3,3,4,3,2,2,2,3,0]
      ’    - decrement              []   [0]    [1,0,-1]           [0,1,0]             [5,4,4,3,3,3,4,3,2,2,1,3,3,2,1,2,1,1,3,2,3,1,2,2,3,2,1,1,1,2,-1]
        Ḃ  - n mod 2                1    1      1                  0                   1
       ×   - multiply               []   [0]    [1,0,-1]           [0,0,0]             [5,4,4,3,3,3,4,3,2,2,1,3,3,2,1,2,1,1,3,2,3,1,2,2,3,2,1,1,1,2,-1]
         Ạ - all truthy?            1    0      0                  0                   1

花了我大约10分钟的时间来理解它是如何工作的……很棒的技术,我想不出一种检查数组是否不包含2的幂的好方法:-)
ETHproductions


6

Python 2,99个字节

lambda n:n&1-any(n-2**k>1and all((n-2**k)%j for j in range(2,n-2**k))for k in range(len(bin(n))-2))

在线尝试!

-4个字节,感谢Leaky Nun

-2个字节感谢Wondercricket

+8个字节来纠正错误

-1字节感谢Xcoder先生

-3字节归功于Einkorn Enchanter

+12个字节来纠正错误


我认为这也是Python 3兼容的答案吗?
阿里·库珀·戴维斯

对于1和所有形式为2 ^ M + 1的de Polignac数,例如262145和2097153(后继的是4722366482869645213697、38685626227668133590597633、519229685853482762853049632922009等),它具有1的假阴性。并不是数字的大问题,因为它可以正确识别262139、262259、2097131和2097187。
Deadcode

1
@Deadcode显式检查,以确保“素数”不为1;现在应该工作
HyperNeutrino

6

正则表达式(ECMAScript),97字节

这个问题为解决非原子超前问题提供了一个有趣的案例。这是到目前为止,我有充分的理由将两个版本的2 test的幂((x+)(?=\2$))*x$和和(?!(x(xx)+)\1*$)放在同一个正则表达式中,这是唯一的理由,也是到目前为止,我唯一需要保护素数测试不匹配的时间1,(?!(xx+)\1+$)xx当在较大的正则表达式中使用时,为。

^(?!(xx)*$|(x+)((?!(xx+)\4+$).*(?=\2$)((x+)(?=\6$))*x$|(?!(x(xx)+)\7*$).*(?=\2$)(?!(xx+)\9+$)xx))

在线尝试!

# For the purposes of these comments, the input number = N.
^
# Assert that N is not the sum of a prime number and a power of 2.
(?!
    (xx)*$                 # Assert that N is odd.
|
    # Since we must cycle through all values for a number X and a corresponding number
    # N-X, this cannot be in an atomic lookahead. The problem then becomes that it
    # consumes characters. Thus we must let X be the larger of the two and N-X be the
    # smaller, and do tests on X followed by tests on N-X. We can't just test X for
    # being prime and N-X for being a power of 2, nor vice versa, because either one
    # could be smaller or larger. Thus, we must test X for being either prime or a
    # power of 2, and if it matches as being one of those two, do the opposite test on
    # N-X.
    # Note that the prime test used below, of the form (?!(xx+)\2+$), has a false match
    # for 0 and 1 being prime. The 0 match is harmless for our purposes, because it
    # will only result in a match for N being a power of 2 itself, thus rejecting
    # powers of 2 as being de Polignac numbers, but since we already require that N is
    # odd, we're already rejecting powers of 2 implicitly. However, the 1 match would
    # break the robustness of this test. There can be de Polignac numbers of the form
    # 2^M+1, for example 262145 and 2097153. So we must discard the 1 match by changing
    # the prime test to "(?!(xx+)\2+$)xx". We only need to do this on the N-X test,
    # though, because as X is the larger number, it is already guaranteed not to be 1.
    (x+)           # \2 = N-X = Smaller number to test for being prime or a power of 2;
                   # tail = X = larger number to test for being prime or a power of 2.
    (
        (?!(xx+)\4+$)      # Test X for being prime.
        .*(?=\2$)          # tail = N-X
        ((x+)(?=\6$))*x$   # Test N-X for being a power of 2. Use the positive version
                           # since it's faster and doesn't have a false match of 0.
    |
        (?!(x(xx)+)\7*$)   # Test X for being a power of 2. Use the negative version
                           # because the testing of X has to be in a lookahead, and
                           # putting the positive version in a positive lookahead would
                           # be worse golf. It doesn't matter that this can have a false
                           # match of 0, because X is guaranteed never to be 0.
        .*(?=\2$)          # tail = N-X
        (?!(xx+)\9+$)xx    # Test N-X for being prime. We must prevent a false match of
                           # 1 for the reason described above.
    )
)

正则表达式(ECMAScript +分子先行),53 52字节

^(?!(xx)*$|(?*xx+(((x+)(?=\4$))*x$))\2(?!(xx+)\5+$))

# For the purposes of these comments, the input number = N.
^
# Assert that N is not the sum of a prime number and a power of 2.
(?!
    (xx)*$                   # Assert that N is odd.
|
    (?*
        xx+                  # Force N - \2 to be > 1, because the prime test used
                             # below has a false match of 1, which would otherwise
                             # give us false negatives on de Polignac numbers of the
                             # form 2^M+1, such as 262145 and 2097153.
        (((x+)(?=\4$))*x$)   # Cycle through subtracting all possible powers of 2 from
                             # tail, so we can then test {N - {power of 2}} for being
                             # prime.
                             # \2 = the power of 2
    )
    \2                       # tail = N - \2
    (?!(xx+)\5+$)            # Test tail for being prime. If it matches, this will fail
                             # the outside negative lookahead, showing that N is not a
                             # de Polignac number.
)

这个版本不仅更简洁,而且速度更快,因为它不必循环所有可能的方法,即N是两个数字的和,而只需循环从N减去2的所有幂,然后测试差值是否为质数即可。

分子前瞻可以轻松地转换为可变长度的后瞻:

正则表达式(.NET或ECMAScript 2018),55 54字节

^(?!(xx)*$|xx+(((x+)(?=\4$))*x$)(?<=(?<!^\5+(x+x))\2))

在线尝试!(.NET)
在线尝试!(ECMAScript 2018)

# For the purposes of these comments, the input number = N.
^
# Assert that N is not the sum of a prime number and a power of 2.
(?!
    (xx)*$                 # Assert that N is odd.
|
    xx+                    # Force N - \2 to be > 1, because the prime test used
                           # below has a false match of 1, which would otherwise
                           # give us false negatives on de Polignac numbers of the
                           # form 2^M+1, such as 262145 and 2097153.
    (((x+)(?=\4$))*x$)     # Cycle through subtracting all possible powers of 2 from
                           # tail, so we can then test {N - {power of 2}} for being
                           # prime.
                           # \2 = the power of 2
    (?<=
        (?<!^\5+(x+x))     # Test tail for being prime. If it matches, this will fail
                           # the outside negative lookahead, showing that N is not a
                           # de Polignac number.
        \2                 # tail = N - \2
    )
)

您的正则表达式可以优化到^(?!(x+)((?!(xx+)\3+$)x*(?!(x(xx)+)\4*$)|x(?!(x(xx)+)\6*$)x*(?!(xx+)\8+$)x)?\1$)没有太多困难。然后,经过深思熟虑,可以进一步打高尔夫球^(?!(x+)((x?)(?!(x(x\3)+)\4+$)x*(?!(x(xx)+|\3\3+)\6+$)\3)?\1$)。可能更短一些
H.PWiz

我的最短
镜头

哦,(x(xx)+|\3\3+)->(x\3?(xx)+)
H.PWiz,

4

Mathematica,41个字节

OddQ@#&&!Or@@PrimeQ[#-2^Range[0,Log2@#]]&

1
没有内置功能吗?哇,我很惊讶
HyperNeutrino

1
这里太烦人了,Mathematica认为负素数是素数,否则您可以通过替换PrimeQ[#-2^Range[0,Log2@#]]PrimeQ[#-2^Range[0,#]]然后由来节省字节PrimeQ[#-2^Range@#/2]
格雷格·马丁



4

Brachylog15 13字节

/₂ℕ|>ṗ;?-₍ḃ+1

在线尝试!

输出de Polignac数最多为1000。

返回false.de Polignac数和true.否则。

基于@LeakyNun删除的答案,并提供了一些错误修正(在其允许的情况下发布)。

(使用@Jonathan Allan的方法检查2个字节,以检查数字是否为2的幂。)

在下列情况下,给定的号码不是de Polignac号码:

/₂ℕ              It's an even number
   |>ṗ           Or there exists a prime number less than the input
      ;?-₍       which when subtracted from the input
          ḃ      gives a result that has a binary form
           +     such that the sum of the bits 
            1    is 1

=h2会短1个字节,但对3任何一个都不起作用。
致命的

给(非移动)自身的注释:14个字节在线尝试!。受到乔纳森·艾伦(Jonathan Allan)的果冻答案的启发。
sundar-恢复莫妮卡


我想提醒您的笔记吗?
Kroppeb

1
@Deadcode它在发布时曾经工作过,与此同时,关于划分的某些事情似乎已经发生了变化-例如。在线尝试!返回false而不是64。更改可能是由于对语言的这种更改,但是我有一段时间没有在这里活动了,所以不知道这是故意的还是bug。
sundar-恢复莫妮卡

3

果冻,13个字节

ÆRạl2=Ḟ$o/o‘Ḃ

在线尝试!

ÆRạl2=Ḟ$o/     Main link; argument is z
ÆR             Generate all primes in the range [2..z]
  ạ            Absolute difference with z for each prime
   l2          Logarithm Base 2
     =Ḟ$       For each one, check if it's equal to its floored value (i.e. if it is an integer)
        o/     Reduce by Logical OR to check if any prime plus a power of two equals the z
          o‘Ḃ  Or it's not an odd number. If it's not an odd number, then automatically return 1 (false).

输出1为false和0true。


Ḷ2*ạfÆRṆ然后检查校验
漏尼姑

@LeakyNun Ḷ2*ạfÆRṆo‘Ḃ返回1两个12722; 那是不对的。除非那不是您的建议。
HyperNeutrino

您需要使用and,而不是or。(或者您可以删除我的最后一个否定词,然后继续执行9/10字节)
Leaky Nun

@LeakyNun您的片段也给出0了149
ETHproductions

@ETHproductions正确。更改_@修复它。
Leaky Nun

2

Perl 6、55个字节

{so$_%2&&$_∉((1,2,4...*>$_) [X+] grep &is-prime,^$_)}

在线尝试!

  • (1, 2, 4 ... * > $_) 是2的幂的序列,直到输入参数为止(Perl从提供的元素中推断出几何级数)。
  • grep &is-prime, ^$_ 是输入参数之前的质数列表。
  • [X+] 评估两个系列的叉积的所有元素的总和。

我可以不用so少两个字节就完成操作,但是随后它返回两个不同的伪造的值(0False)。


2

公理,86字节

f(n:PI):Boolean==(~odd?(n)=>false;d:=1;repeat(n<=d or prime?(n-d)=>break;d:=d*2);n<=d)

测试与结果

(21) -> for i in 1..600 repeat if f(i) then output i
   1
   127
   149
   251
   331
   337
   373
   509
   599

2

Haskell,104102字节

p x=[x]==[i|i<-[2..x],x`mod`i<1]
h k|even k=1>2|2>1=notElem k$((+)<$>(2^)<$>[0..k])<*>filter(p)[1..k]

说明

  • p是一个查找素数的函数(效率很低!)
  • 创建(+)应用于2 ^部分功能的列表,该列表应用于列表[0..input]
  • 将以上内容应用于过滤后的列表1以输入素数
  • 搜索所有可能值的笛卡尔积,以确保笛卡尔积中不存在输入
  • 保护以确保偶数输入自动为假。

更新:向Einkorn Enchanter大喊两个字节!


1
p x=[x]==[i|i<-[2..x],x`mod`i<1]是较短的素数测试。
小麦巫师

@EinkornEnchanter大收获!你打了我两个字节!
maple_shaft

1
您也可以filter p[1..k]代替filter(p)[1..k]
Wheat

1

普通Lisp,134个字节

(lambda(x)(flet((p(x)(loop for i from 2 below x always(>(mod x i)0))))(or(evenp x)(do((j 1(* j 2))(m()(p(- x j))))((or(>= j x)m)m)))))

NIL当参数为Polignac数时返回,T否则返回。

在线尝试!

取消高尔夫:

(lambda (n)
  (flet ((prime? (x)                 ; x is prime
           loop for i from 2 below x ; if for i in [2..n-1]
           always (> (mod x i) 0)))  ; is x is not divisible by i
    (or (evenp n)                    ; if n is even is not a Polignac number
        (do ((j 1( * j 2))           ; loop with j = 2^i, i = 0, 1,... 
             (m () (prime? (- n j)))); m = n - 2^i is prime?
            ((or (>= j n)            ; terminate if 2^i ≥ n
                 m)                  ; or if n - 2^i is prime
             m)))))                  ; not a polignac if n - 2^i is prime

1

APL(Dyalog扩展),12字节

2∘|⍲0⍭⊢-2*…

在线尝试!

匿名前缀默认功能。返回1表示真实,0表示虚假。

很大程度上基于ETHProductions的Japt答案

感谢@Adám帮助我打高尔夫球,并为此制作了Dyalog Extended。

怎么样:

2∘|⍲0⍭⊢-2*…    Tacit prefix function; input will be called 
                Inclusive Range [0..⍵]
         2*      2 to the power of each number in the range
       ⊢-        Subtract each from 
     0          Non-primality check on each resulting number
                Logical NAND
 2∘|             Mod 2
                Not any (bitwise OR reduction, then negated)




0

APL(NARS) 80 chars, 160 bytes

∇r←f w;n
r←¯1⋄→0×⍳(w≤0)∨w≥9E9⋄r←0⋄→0×⍳0=2∣w⋄n←r←1
→0×⍳w≤n⋄→3×⍳0πw-n⋄n×←2⋄→2
r←0
∇

The function 0π is the function that return its argument is prime or not. For me this function has not be recursive so it is a little more long... Test:

  {1=f ⍵:⍵⋄⍬}¨1..1000
1  127  149  251  331  337  373  509  599  701  757  809  877  905  907  959  977  997 

对于输入<= 0或输入> = 9E9,它返回¯1(错误)

  f¨0 ¯1 ¯2 900000000001
¯1 ¯1 ¯1 ¯1 

0

C#(Visual C#交互式编译器),107字节

x=>{var r=Enumerable.Range(2,x);return x%2>0&r.All(p=>r.Any(d=>d<p&p%d<1)|r.All(n=>x!=p+Math.Pow(2,n-2)));}

在线尝试!

不是最有效的代码,但似乎可以工作。我的原始解决方案在对公式中的素数进行测试之前已过滤了素数,并且其性能要好得多。当前版本短11个字节。

// input x is an integer
x=>{
  // we need to generate several
  // ranges. since this is verbose,
  // generate 1 and keep a reference
  var r=Enumerable.Range(2,x);
  // this is the main condition
  return
     // input must be odd
     x%2>0&
     // generate all possible p's
     // over our range and ensure that
     // they satisfy the following
     r.All(p=>
       // either p is not prime
       r.Any(d=>d<p&p%d<1)
       |
       // or there is no n that satisfies
       // the formula: x=p+2^n
       r.All(n=>x!=p+Math.Pow(2,n-2))
     );
}
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.