การนับ +1 จำนวนเฉพาะ


25

กำหนดว่า natural number pคือ+1 ไพร์มของ natural number nถ้าpเป็นจำนวนเฉพาะและการแทนฐานแบบไบนารี่มาตรฐาน (เช่นไม่มีเลขศูนย์นำหน้า) ของpสามารถรับได้โดยการเพิ่ม (เช่นการเติมต่อท้ายหรือแทรก) เดียว1ไปแทน binary มาตรฐานn

ยกตัวอย่างเช่นฐานเป็นตัวแทนของ17คือ10001 2 หมายเลขธรรมชาติแตกต่างกันที่สามารถเกิดขึ้นโดยการเพิ่ม1ที่จะ10001 2มี110,001 2หรือ49 , 101001 2หรือ41 , 100101 2หรือ37และ100,011 2หรือ35

กลุ่มคนเหล่านี้41และ37เป็นหมายเลขเฉพาะดังนั้น17จึงมี+1 จำนวนสองครั้ง

งาน

เขียนโปรแกรมหรือฟังก์ชั่นที่รับจำนวนเต็มบวกnอย่างเคร่งครัดเป็นอินพุตแล้วพิมพ์หรือส่งกลับจำนวน+1 ที่ซ้ำกันของn

อินพุตและเอาต์พุตต้องเป็นเลขจำนวนเต็มหรือการแทนค่าสตริงหรือทศนิยม

ใช้กฎมาตรฐานของ

กรณีทดสอบ

Input:  4
Output: 0

Input:  1
Output: 1

Input:  17
Output: 2

Input:  33
Output: 3

Input:  553
Output: 4

Input:  3273
Output: 5

Input:  4145
Output: 6

Input:  4109
Output: 7

Input:  196869
Output: 8

1
เย็น! ถ้าฉันมีเวลาคืนนี้ฉันจะตอบตอนนี้
anOKsquirrel

คำตอบ:


5

Pyth, 20 ไบต์

s/LPd{mij\1c.BQ]d2hQ

ชุดทดสอบ

s/LPd{mij\1c.BQ]d2hQ
                        Q = eval(input())
      m           hQ    For insertion position in [0 ... Q]
            .BQ         Convert Q to binary string
           c   ]d       Chop at insertion position
        j\1             Join on '1'
       i         2      Convert to integer
     {                  Deduplicate
 /LPd                   Map each number to the number of times it occurs in its
                        prime factorization, e.g. whether or not it is prime.
s                       Sum and print.

1
หืม "deduplicate" เป็นคำเดียว
lirtosiast

8

JavaScript ES6, 141 bytes 143 147 160

บันทึก 13 ไบต์ด้วย @Naouak

n=>[...t=n.toString(2)].map((l,i)=>t.slice(0,v=i+1)+1+t.slice(v)).filter((l,i,a)=>a.indexOf(l)==i&&(p=(n,c)=>n%c&&c>n-2||p(n,++c))('0b'+l,2))

วิธีการที่คล้ายกับคำตอบ TeaScript ของฉันใช้ RegExp (คุณได้ยินฉันถูก) เพื่อตรวจสอบช่วงเวลา

Ungolfed

n=>
   [...t = n.toString(2)]                  // To binary
   .map((l,i)=>                            // Make cycles
               t.slice(0, v = i+1)
               + 1
               + t.slice(v)
   ).filter((l,i,a)=>  
                     a.indexOf(l) == i &&  // Remove Duplicates
                     (p=(n,c)=>            // Prime checking
                               n % c &&
                                 c > n - 2 ||
                                 p(n,++c)
                     )('0b'+l,2)
   ).length

ฉันคิดว่าคุณสามารถตรวจสอบนายกแบบสั้น ๆ เช่นนี้(p=(n,c)=>n%c!=0?c>=n-1?1:p(n,++c):0)('0b'+l,2)แทน!Array(+('0b'+l)+1).join(1).match(/^1?$|^(11+?)\1+$/)
Naouak

@Naouak สุดยอดที่บันทึก 13 ไบต์! :)
Downgoat

4

Minkolang 0.11 , 54 52 ไบต์

n1(2*d0c`,)(0c1c$%$r2*1c*1c++1g2:d1G)rxSI1-[0g2M+]N.

คำอธิบาย

n             Get integer from input (let's call it n)
1(       )    Get the smallest power of 2 (say, k) greater than input (starting with 1)
  2*d         Multiply by 2 and duplicate
     0c`,     Copy n and see if it's greater (while loop breaks on 0)

(0c1c$%                       Copy n, copy k, and divmod (pushes n//k, n%k)
       $r                     Swap top two elements
         2*                   Multiply by 2
           1c*                Copy k and multiply
              1c+             Copy k and add
                 +            Add
                  1g2:        Get k and divide by 2
                      d1G)    Duplicate and put one copy back in its former place

rx            Reverse and dump (dumps n and top of stack is now 0)
S             Remove duplicates
I1-[     ]    Check each element for primality
    0g        Get potential prime from bottom of stack
      2M      1 if prime, 0 otherwise
        +     Add (this is why I didn't dump the left-over 0 earlier)
N.            Output as integer and stop.

ฉันตื่นเต้นเสมอที่จะพูดกับรุ่นมิงโกลังอีกรุ่นหนึ่ง
Conor O'Brien

4

TeaScript ขนาด 22 ไบต์

x÷¿®x÷E(i¬,1)¤©d¡F(¥)n

TeaScript เริ่มดูเหมือน APL ... อักขระพิเศษจะถูกแปลงเป็นลำดับที่ยาวกว่าและซ้ำกันบ่อยๆ

ล่ามออนไลน์ต้องแน่ใจว่าได้ตรวจสอบ "อินพุตเป็นตัวเลข"

คำอธิบาย && ไม่อัปโหลด

xT(2)s``m(#P(xT(2)E(i+1,1),2))d()F($P)n

xT(2)      // Take input, convert to binary
s``m(#     // Loop over input

  P(         // Convert to decimal...
     xT(2)     // Input to binary
     E(i+1,1)  // Inset 1 into (above) at current index in loop
  ,2)    

)d()       // Remove duplicates
F($P)      // Filter items that aren't prime
n          // Grab length.

เป็น 31 ไบต์โดยใช้ gedit บน Xubuntu
Glen O

1
มีขนาด 31 ไบต์พร้อมการเข้ารหัส UTF-8 แต่ 22 ไบต์พร้อม ISO-8859-1
เดนนิส

4

จูเลีย55 55ไบต์

n->sum(isprime,∪(2n+(k=2.^(0:endof(bin(n))))-n%k))

k=2.^(0:endof(bin(n)))สร้างอาร์เรย์ที่มีอำนาจของ 2 ตั้งแต่ 1 nถึงอำนาจสูงสุดน้อยกว่า 2n+k-n%kจากนั้นใช้การดำเนินการอาร์เรย์เพื่อพิจารณา "+1 ตัวเลข" ที่เป็นไปได้ทั้งหมด (เทียบเท่ากับunionซึ่งทำสิ่งเดียวกันกับuniqueในสถานการณ์นี้) ลบค่าการทำซ้ำ จากนั้นsum(isprime,)นับจำนวนช่วงเวลาในรายการ


4

CJam, 26 ไบต์

ไม่ได้เป็นผู้ชนะ แต่มันเต้น CJam e\ที่มีอยู่คำตอบค่อนข้างแน่นหนาและมันเป็นครั้งแรกที่ฉันได้ไปใช้คำสั่ง

1ri2b+_,{_)e\_}%_&{2bmp},,

ทดสอบที่นี่

คำอธิบาย

1       e# Push a 1 (this is the 1 we'll be inserting everywhere).
ri      e# Read input and convert to integer.
2b      e# Convert to base 2.
+       e# Prepend the 1.
_,      e# Duplicate and get the number of bits N.
{       e# Map this block over i from 0 to N-1...
  _)    e#   Create a copy and increment to i+1.
  e\    e#   Swap the bits at positions i and i+1, moving the 1 one step through the array.
  _     e#   Duplicate so we keep this version on the stack.
}%
_&      e# Remove duplicates via set intersection with itself.
{       e# Filter the remaining digit lists based on this block...
  2b    e#   Convert bits back to an integer.
  mp    e#   Test for primality.
},
,       e# Get the length of the remaining list.

สิ่งหนึ่งที่ควรค่าแก่การสังเกตคือเราสลับบิตที่0และ1ก่อนที่จะทำการคัดลอกครั้งแรกดังนั้นเราจึงสูญเสียอาเรย์ดั้งเดิมที่มีการเติม1ไว้ด้านหน้า อย่างไรก็ตามอินพุตจะเป็นค่าบวกเสมอดังนั้นเลขนำหน้าจะเป็นหนึ่ง นั่นหมายถึงหลังจากที่เตรียมอีกหนึ่งรายการหลักจะเริ่มต้นด้วยเสมอ[1 1 ...]ดังนั้นการแลกเปลี่ยนครั้งแรกจะไม่มีตัวเลือกในกรณีใด ๆ



3

Julia, 110 108 104 87 ไบต์

n->sum(i->isprime(parse(Int,i,2)),(b=bin(n);∪([b[[1:i;1;i+1:end]]for i=1:endof(b)])))

สิ่งนี้จะสร้างฟังก์ชั่นที่ไม่มีชื่อที่ยอมรับและจำนวนเต็มและส่งกลับจำนวนเต็ม f=n->...เรียกว่าให้มันชื่อเช่น

Ungolfed:

function f(n::Integer)
    # Get the binary representation of n as a string
    b = bin(n)

    # Construct an array consisting of binary strings with
    # a one prepended, appended, and each insertion
    x = [b[[1:i; 1; i+1:end]] for i = 1:endof(b)]

    # Count the number of primes
    c = sum(i -> isprime(parse(Int, i, 2)), unique(x))

    return c
end

บันทึกแล้ว 17 ไบต์ขอบคุณ Glen O!


binมีการเริ่มต้นด้วยเลข 1 "1"bดังนั้นคุณจึงไม่จำเป็นต้องจับแยก และเมื่อใดi=length(b)คุณจะมีความb[i+1:end]เทียบเท่า""ดังนั้นจึงไม่จำเป็นต้องมีรายการนั้น (เพียงแค่ต้องจัดการb=bin(n)ในบางจุด) และsumจะทำเช่นเดียวกันกับcountสองไบต์ที่น้อยกว่า
เกลน O

นอกจากนี้เนื่องจากคุณจะใช้ช่วงที่มีความยาวbอยู่ดีคุณอาจได้มันมาพร้อมกับกลอุบายเล็กน้อยb=bin(n)[s=1:end]และfor i=sเพื่อความเข้าใจ
เกลน O

นอกจากนี้คุณยังสามารถบันทึกไบต์อื่นได้โดยใช้ความจริงที่ว่าบิตแรกbinควรเป็น 1 และคุณจะได้รับสิ่งนี้: n->sum(i->isprime(parse(Int,i,2)),(b=bin(n);unique([b[[1:i;1;i+1:end]]for i=1:endof(b)])))- สิ่งนี้จะทำให้จำนวนนับถึง 90 ไบต์
เกลน O

ในความเป็นจริงเอาออกอีกหนึ่งไบต์โดยแทนที่uniqueด้วยunion- มันจะทำสิ่งเดียวกันถ้าให้อาร์เรย์เดียวเป็นอินพุต หรือดีกว่ายังแทน union
เกลน O

@GlenO คุณเป็นปรมาจารย์ ขอบคุณ先生!
Alex A.

2

CJam, 58 ไบต์

L{:TQ\=A+Q\+TWe\-2<s:~2b}q~2b0+:Q,(%{:BLe=!{B+:L}&}%~:mp:+

นี่ใช้เวลาหนึ่งวันและนี่คือการทำซ้ำครั้งที่ 4 ของฉัน



1

PHP, 145 ไบต์

ฉันได้เพิ่มบรรทัดใหม่เพื่อให้สามารถอ่านได้:

function($n){for($b=base_convert;++$i<=strlen($s=$b($n,10,2));$r+=!$s[$i]&$k<=$j)
for($j=1;($k=$b(substr_replace($s,1,$i,0),2,10))%++$j;);echo$r;}


1

APL, 55

{+/{2=+/0=⍵|⍨⍳⍵}¨∪⍵{2⊥(⍵↑X),1,⍵↓X←⍺⊤⍨N⍴2}¨-1-⍳N←1+⌊2⍟⍵}

เวอร์ชันของ Dyalog ที่สั้นลง 2 ไบต์:

{+/2=+/¨0=|⍨∘⍳⍨¨∪⍵{2⊥⍵(↑,(1∘,↓))⍺⊤⍨N⍴2}¨-1-⍳N←1+⌊2⍟⍵}

1

Matlab (120)

n=input('');a=dec2bin(n);g=arrayfun(@(x)bin2dec(cat(2,a(1:x),49,a(x+1:end))),1:log2(n));nnz(unique(g(find(isprime(g)))))

  • กำลังเล่นกอล์ฟเพิ่มเติม ...

1

Brachylogขนาด 17 ไบต์

{ḃ~c₂{,1}ʰc~ḃṗ}ᶜ¹

ลองออนไลน์!

อินพุตผ่านตัวแปรอินพุตและเอาต์พุตผ่านตัวแปรเอาต์พุต

{             }ᶜ¹    Count every unique
             ṗ       prime number
           ~ḃ        the binary representation of which is
 ḃ                   the binary representation of the input
  ~c₂                partitioned into two (possibly empty) lists
     {  }ʰ           with the first list
      ,1             having a 1 appended
          c          and the two lists then being concatenated back into one.


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.