ซ้ำจำนวนเฉพาะ


13

ลำดับอื่นความท้าทายอื่น *

คำนิยาม

นายกpอยู่ในลำดับนี้ขอเรียกว่าA, IFF ทุกหลักdในp's ขยายตัวทศนิยมคุณแทนที่dด้วยdสำเนาdและเลขที่ส่งผลให้ยังคงเป็นนายก; ไม่อนุญาตศูนย์

ตัวอย่างเช่น11มีเพียงเล็กน้อยในลำดับนี้ (เป็นหมายเลขแรกโดยไม่ได้ตั้งใจ) ลำดับถัดไปคือ31เนื่องจาก3331เป็นตำแหน่งเฉพาะเช่นกัน แล้วก็53เพราะ55555333มันก็เป็นนายกและอื่น ๆ

ท้าทาย

รับอินพุตn, ส่งคืนA(n), คือnไอเท็ม th ในลำดับนี้

ตัวอย่าง

ต่อไปนี้เป็นคำศัพท์ 20 คำแรกที่ให้คุณเริ่มต้นได้ นี่คือA057628สำหรับ OEIS

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

นี่หมายถึงA(0) = 11, A(1) = 31ฯลฯ เมื่อใช้การทำดัชนีเป็นศูนย์

กฎระเบียบ

  • คุณสามารถเลือกการจัดทำดัชนีแบบศูนย์หรือหนึ่งรายการ โปรดระบุคำตอบของคุณ
  • แทนที่จะส่งกลับnองค์ประกอบที่เพียงคุณสามารถเลือกที่จะกลับnเงื่อนไขแรก
  • คุณสามารถสมมติว่าอินพุต / เอาต์พุตจะไม่ใหญ่กว่ารูปแบบจำนวนเต็มดั้งเดิมของภาษาของคุณ อย่างไรก็ตามหลักที่มีการซ้ำหลักอาจมีขนาดใหญ่กว่ารูปแบบดั้งเดิมของภาษาของคุณดังนั้นจะต้องนำมาพิจารณา
  • ตัวอย่างเช่น1871จำนวนสุดท้ายของตัวอย่างมีจำนวนเฉพาะที่สอดคล้องกัน18888888877777771ซึ่งค่อนข้างใหญ่กว่า INT32 มาตรฐานเล็กน้อย
  • ยอมรับได้ทั้งโปรแกรมหรือฟังก์ชั่น หากฟังก์ชั่นคุณสามารถส่งคืนผลลัพธ์มากกว่าการพิมพ์
  • เอาต์พุตสามารถไปที่คอนโซลส่งคืนจากฟังก์ชันแสดงขึ้นในป๊อปอัพการแจ้งเตือน ฯลฯ
  • ช่องโหว่มาตรฐานเป็นสิ่งต้องห้าม
  • นี่คือเพื่อให้ใช้กฎการเล่นกอล์ฟตามปกติทั้งหมดและรหัสที่สั้นที่สุด (เป็นไบต์) ชนะ

* เพื่อความเป็นธรรมฉันได้ทำข้อตกลงสองสามข้อแรกของลำดับที่เล่นกับตัวเลขและจากนั้นก็ไปที่ OEIS เพื่อรับลำดับที่เหลือ


2
ฉันสงสัยว่ามีไพรม์ที่มีผลลัพธ์ตัวเลขซ้ำอยู่ในลำดับนี้หรือไม่และมีผลลัพธ์หลักซ้ำอยู่ในลำดับนี้หรือไม่และที่อื่น ๆ ก็คืออินฟินิตี้ ดูเหมือนไม่น่าเป็นไปได้สูง
Steadybox

1
@Steadybox 11 เป็นไปตามเงื่อนไขนี้ แต่นอกเหนือจากนั้นมันน่าสนใจที่จะดูว่าคุณสามารถใช้การดำเนินการซ้ำหลายหลักและรับช่วงเวลาได้กี่ครั้ง
dylnan

ระบุว่า 1666666999999999 มีความสำคัญทำไมจึงไม่อยู่ในลำดับที่ 169?
Pablo Oliva

2
@PabloOliva เพราะตัวเองไม่ได้ที่สำคัญมันเป็น169 13 * 13
AdmBorkBork

คำตอบ:


6

Huskขนาด 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 บันทึกข้อมูลเป็นไบต์ การใช้แทนεpจะช่วยประหยัดอีกไบต์ แต่นั่นทำให้โปรแกรมช้าลงเมื่อเทียบกับ n = 2


1
@ H.PWiz ผมไม่คิดว่าเราตัดสินกับความเร็วที่นี่ ...
เอริก Outgolfer

ฉันควรเร่งล่ามให้เร็วขึ้นจริง ๆมันบ้าว่ามันช้ากว่าการค้นหาปัจจัยสำคัญทั้งหมด ...
Zgarb

6

05AB1E , 14 13 ไบต์

-1 ไบต์ต้องขอบคุณEmigna !

µ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Ạµ#

ลองออนไลน์!

นาย Xcoder: -1 ไบต์ (ตรรกะทั้งหมด)

Erik the Outgolfer: -2 Bytes (หนึ่งบรรทัดแทนสอง)

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

แก้ไข: เดิมส่งคำตอบที่มีตัวเลขเป็น 0 ในการแทนทศนิยมซึ่งไม่ได้รับอนุญาตโดยเฉพาะ


ฉันพยายามหาคำตอบที่สั้นกว่าและเป็นอิสระ แต่ฉันเพิ่งได้สิ่งเดียวกัน :( xD
HyperNeutrino


4

อลิซ , 72 70 66 62 56 ไบต์

ขอขอบคุณลีโอสำหรับการบันทึก 5 ไบต์

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

ลองออนไลน์!

ใช้อินพุตที่อิง 1

คำอธิบาย

สนามกอล์ฟที่เคล็ดลับ neatest นี่ (แม้ว่ามันจะเพียงช่วยประหยัดคู่ของไบต์) คือว่าฉันใช้ primality ทดสอบซึ่งจะช่วยให้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 ไบต์

  • ขอบคุณ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))

ลองออนไลน์!


Python 2 , 195 179 167 140 138 136 135 134 ไบต์

  • บันทึกแล้ว 27 ไบต์ขอบคุณovs ; ใช้xrangeแทนrangeดังนั้นหลีกเลี่ยงMemoryErrorและกระชับหน้าที่สำคัญ; ปรับปรุงการนับดัชนีจำนวนเต็ม
  • บันทึกสองไบต์ โดยใช้ท่อไบนารีหรือการดำเนินการที่จะประหยัดกว่าไบต์|or
  • บันทึกสองไบต์ ย้อนกลับไปยังฟังก์ชั่นเฉพาะและทำการจัดการเชิงตรรกะเพิ่มเติม
  • บันทึกเป็นไบต์ ใช้~-แทน0**เพื่อกลับดำรงอยู่ของศูนย์ในjขณะที่&ตามด้วยบูลจริงแยกคุณสมบัติบูลของค่านี้
  • บันทึกขอบคุณไบต์ลินน์ ; กอล์ฟ~-A&B&C(เทียบเท่า(not A) and B and C) กับA, B, Cการเป็น booleans A<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ลำดับชุดปัจจุบัน canditate (ตั้งใจที่9จะปรับปรุงประสิทธิภาพในขณะที่รักษาขนาดของโปรแกรม) และฟังก์ชั่นการตรวจสอบที่สำคัญ
ตราบใดที่nไม่เป็นศูนย์nรายการลำดับที่ -th จะยังไม่พบ ดังนั้นjจะเพิ่มขึ้นและnลดลงโดยหนึ่ง iff jเป็นตัวเลขที่ตอบสนองคุณสมบัติที่ต้องการ
เมื่อวงสิ้นสุดลงjเป็นnรายการลำดับ -th และพิมพ์จึง


ฉันมาสายไปงานเลี้ยง แต่คุณสามารถโกนได้อีก 4 ไบต์
ArBo

@ArBo ขอบคุณ
Jonathan Frech

3

Pyth , 21 ไบต์

.f&.AKjZT&P_ss*VK`ZP_

ลองที่นี่!

ค่อนข้างยาวเป็น Pyth ไม่มีการขยายทศนิยมในตัว

  • รับจำนวนเต็มบวกN ตัวแรก( .f) ที่:
    • มีตัวเลขทั้งหมดที่เป็นจริง ( .AKjZT) และ ( &) ...
    • การคูณเวกเตอร์ของการแทนค่าสตริงด้วยตัวเลข ( *VK`Z), รวมเข้าด้วยกันและแปลงเป็นจำนวนเต็ม ( ss) เป็นจำนวนเฉพาะ ( P_) และ ( &) ...
    • นั่นเป็นช่วงเวลาที่เหมาะสม ( P_)

คุณสามารถลบได้eตามการแก้ไขกฎใหม่
Erik the Outgolfer

@EriktheOutgolfer เรียบร้อยแล้วขอบคุณ
Mr. Xcoder

2

Perl 6 , 51 ไบต์

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

ลองออนไลน์!

  • grep {...}, 2..*กรองลำดับอนันต์ของตัวเลขธรรมชาติเริ่มต้นจาก 2 โดยใช้ฟังก์ชันเพรดิเคตระหว่างเครื่องมือจัดฟัน ดัชนีลงในรายการกรองนี้โดยใช้อาร์กิวเมนต์ของฟังก์ชัน(...)[$_]$_
  • !/0/ กรองตัวเลขที่มีตัวเลขศูนย์
  • S:g/./{$/ x $/}/ ทำซ้ำแต่ละหลักในการขยายทศนิยมของหมายเลขทดสอบ
  • is-prime $_ & S:g/./{$/ x $/}/เรียกใช้is-primeฟังก์ชันในตัวพร้อมกับและทางแยกของ$_หมายเลขทดสอบและหมายเลขที่เกิดจากการจำลองตัวเลข ฟังก์ชั่นจะกลับมาจริงถ้าทั้งสมาชิกของและแยกเป็นนายก

2

J, 81 ไบต์

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

นี่เป็นหนึ่งในสถานการณ์ที่ฉันยังไม่พบวิธีแก้ปัญหา J ที่ดี

อย่างไรก็ตามฉันโพสต์สิ่งนี้ด้วยความหวังว่าจะได้เรียนรู้สิ่งใหม่

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. ซึ่งหมายความว่าเราต้องใช้ไบต์ที่มีค่ามากมายสำหรับงานง่าย ๆ ในการระบุ args ( [และ]) และเอารายการองค์ประกอบ 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#])&.":ซึ่งน่าเสียดายที่ไม่สามารถแยกค่า primes ด้วย '0' ได้ คุณมีความคิดใด ๆ นอกจากนี้ยังมีที่จะมี'x',~ส่วนหนึ่งที่จะได้รับความแม่นยำเป็นพิเศษ ...
โคล

@ โคลใช่อีกครั้ง: ฟังก์ชั่นผู้ช่วยเพิ่มไบต์ แต่ ณ จุดนี้เรากำลังขัดทองเหลืองในไททานิคดังนั้นฉันคิดว่าทำไมรำคาญเพียงแค่รักษาความชัดเจนและอาจไมล์หรือ FrownyFrog จะลงเอยด้วยความคิดที่ช่วยประหยัดไบต์ที่แท้จริง
โจนาห์

ฉันจะตรวจสอบการเล่นกอล์ฟของฟังก์ชั่นผู้ช่วยของคุณในภายหลัง
Jonah

จนถึง 57 ไบต์(((0>.-)((*&(1&p:)0&e.|10#.#~),.&.":))([,(+*)~)])/^:_@,&2ใช้10xเพื่อขยายช่วงมิฉะนั้น n = 15 จะข้าม 937
ไมล์

@miles คุณคือพระเจ้าเจ พบเทคนิคใหม่ที่ดีอยู่แล้วที่นี่ จะตรวจสอบอีกครั้งในวันพรุ่งนี้เพื่อให้แน่ใจว่าฉันเข้าใจการวนซ้ำ / ลดลง ฉันไม่ทราบว่าคุณสังเกตเห็นลิงก์ไปยังคำถาม SO ของฉันหรือไม่ แต่คุณจะบอกว่านี่เป็นเทคนิคทั่วไปที่สามารถแก้ปัญหาที่เกิดขึ้นได้หรือไม่
Jonah
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.