หมายเลขคอมโพสิตเดิม


16

นิยามลำดับ

สร้างลำดับของจำนวนเต็มบวกa(n)ดังนี้

  1. a(0) = 4
  2. ระยะแต่ละa(n)อื่น ๆ กว่าครั้งแรกเป็นจำนวนน้อยที่สุดที่ตอบสนองความต่อไปนี้:
    ก) a(n)เป็นจำนวนคอมโพสิต
    ข) a(n) > a(n-1)และ
    ค) เป็นจำนวนคอมโพสิตสำหรับแต่ละa(n) + a(k) + 10 <= k < n

a(0) = 4ดังนั้นเราจึงเริ่มต้นด้วย รายการต่อไปจะต้องเป็นa(1) 9ไม่สามารถเป็นได้5หรือ7เนื่องจากไม่มีคอมโพสิตและไม่สามารถเป็น6หรือ8เพราะ6+4+1=11ไม่ได้ประกอบและ8+4+1=13ไม่ได้ประกอบ สุดท้ายซึ่งเป็นคอมโพสิตดังนั้น9+4+1=14a(1) = 9

รายการต่อไปa(2)จะต้องเป็น10เพราะมันเป็นจำนวนที่เล็กที่สุดที่มีขนาดใหญ่กว่าและ9มีทั้งคอมโพสิต10+9+1=2010+4+1=15

สำหรับรายการถัดไป11และ13ทั้งคู่ก็ออกเพราะพวกเขาไม่ได้ประกอบ 12ออกเนื่องจาก12+4+1=17ไม่ประกอบ 14ออกเนื่องจาก14+4+1=19ไม่ประกอบ ดังนั้น15เป็นระยะต่อไปของลำดับเนื่องจาก15เป็นคอมโพสิตและ15+4+1=20, 15+9+1=25และทุกคนในแต่ละคอมโพสิตดังนั้น15+10+1=26a(3) = 15

นี่คือ 30 คำแรกในลำดับนี้:

4, 9, 10, 15, 16, 22, 28, 34, 35, 39, 40, 46, 52, 58, 64, 70, 75, 76, 82, 88, 94, 100, 106, 112, 118, 119, 124, 125, 130, 136

นี่คือOEIS A133764

ท้าทาย

กำหนดจำนวนเต็มอินพุตnเอาต์พุตnคำที่ตามลำดับนี้

กฎระเบียบ

  • คุณสามารถเลือกการจัดทำดัชนีแบบ 0- หรือ 1 ก็ได้ กรุณาระบุว่าในการส่งของคุณ
  • อินพุตและเอาต์พุตสามารถสันนิษฐานว่าเหมาะสมกับชนิดจำนวนเต็มดั้งเดิมของภาษาของคุณ
  • อินพุตและเอาต์พุตจะได้รับโดยวิธีการที่สะดวกใด
  • ยอมรับได้ทั้งโปรแกรมหรือฟังก์ชั่น หากฟังก์ชั่นคุณสามารถส่งคืนผลลัพธ์มากกว่าการพิมพ์
  • ช่องโหว่มาตรฐานเป็นสิ่งต้องห้าม
  • นี่คือเพื่อให้ใช้กฎการตีกอล์ฟตามปกติและรหัสที่สั้นที่สุด (เป็นไบต์) ชนะ

3
หัวข้อ:หมายเลขที่เคยรู้จักกันในชื่อคอมโพสิต
Magic Octopus Urn

@ MagicOctopusUrn หากนี่มีบางอย่างเกี่ยวกับศิลปะหรือดนตรีฉันจะไปกับมัน แต่ฉันจะยึดติดกับชื่อที่ฉันมีอยู่ในปัจจุบัน
AdmBorkBork

เป็นเรื่องตลกมากขึ้น;)
Magic Octopus Urn

คำตอบ:


5

Husk , 11 ไบต์

!üȯṗ→+fotpN

1 การจัดทำดัชนี ลองออนไลน์!

คำอธิบาย

!üȯṗ→+fotpN  Implicit input, a number n.
          N  The list of positive integers [1,2,3,4,..
      f      Keep those
         p   whose list of prime factors
       ot    has a nonempty tail: [4,6,8,9,10,12,..
 ü           De-duplicate wrt this equality predicate:
     +       sum
    →        plus 1
  ȯṗ         is a prime number.
             Result is [4,9,10,15,16,..
!            Get n'th element.

2

Perl 6 , 70 ไบต์

{(4,->+_{first {none($^a X+0,|(_ X+1)).is-prime},_.tail^..*}...*)[$_]}

ลองทำดัชนี 0

ขยาย:

{  # bare block lambda with implicit parameter $_

  (  # generate the sequence

    4, # seed the sequence

    -> +_ { # pointy block that has a slurpy list parameter _ (all previous values)

      first

      {  # bare block with placeholder parameter $a

        none(                 # none junction
            $^a               # placeholder parameter for this inner block
          X+                
            0,                # make sure $a isn't prime
            |( _ X+ 1 )       # check all a(k)+1
        ).is-prime            # make sure none are prime
      },

      _.tail ^.. *            # start looking after the previous value
    }

    ...                       # keep generating values until

    *                         # never stop

  )[$_]                       # index into the sequence
}


2

JavaScript (ES6), 83 ไบต์

1 การจัดทำดัชนี

f=(n,a=[-1,p=4])=>a[n]||f(n,a.some(x=>(P=n=>n%--x?P(n):x<2)(x-=~p),p++)?a:[...a,p])

การสาธิต

แสดงความคิดเห็น

ฟังก์ชันผู้ช่วยP () , คืนค่าจริงถ้าnเป็นจำนวนเฉพาะ, หรือเป็นเท็จมิฉะนั้น:

P = n => n % --x ? P(n) : x < 2

หมายเหตุ: มันจะต้องถูกเรียกด้วยx = nn

ฟังก์ชั่นหลักf () :

f = (               // given:
  n,                //   n = target index
  a = [-1, p = 4]   //   a = computed sequence with an extra -1 at the beginning
) =>                //   p = last appended value
  a[n] ||           // if a[n] exists, stop recursion and return it
  f(                // otherwise, do a recursive call to f() with:
    n,              //   n unchanged
    a.some(x =>     //   for each value x in a[]:
      P(x -= ~p),   //     rule c: check whether x + p + 1 is prime
                    //     rule a: because a[0] = -1, this will first compute P(p)
      p++           //     rule b: increment p before the some() loop starts
    ) ?             //   end of some(); if truthy:
      a             //     p is invalid: use a[] unchanged
    :               //   else:
      [...a, p]     //     p is valid: append it to a[]
  )                 // end of recursive call



0

Java 8, 186 173 ไบต์

n->{int a[]=new int[n+1],r=a[n]=4;a:for(;n>0;)if(c(++r)<2){for(int x:a)if(x>0&c(r-~x)>1)continue a;a[--n]=r;}return r;}int c(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

0 การจัดทำดัชนี
น่าเสียดายที่การตรวจสอบที่สำคัญ (หรือการตรวจสอบต่อต้าน / นายกรัฐมนตรี / คอมโพสิตในกรณีนี้) ไม่ถูกใน Java ..

คำอธิบาย:

ลองออนไลน์

n->{                     // Method with integer as both parameter and return-type
  int a[]=new int[n+1],  //  Integer-array of size `n+1`
      r=a[n]=4;          //  Start the result and last item at 4
  a:for(;n>0;)           //  Loop as long as `n` is larger than 0
    if(c(++r)<2){        //   Raise `r` by 1, and if it's a composite:
      for(int x:a)       //    Inner loop over the array
        if(x>0           //     If the item in the array is filled in (non-zero),
           &c(r-~x)>1)   //     and if `r+x+1` is a prime (not a composite number):
          continue a;}   //      Continue the outer loop
      a[--n]=r;}         //    Decrease `n` by 1, and put `r` in the array
  return r;}             //  Return the result

// Separated method to check if a given number is a composite number
// (It's a composite number if 0 or 1 is returned, otherwise it's a prime.)
int c(int n){for(int i=2;i<n;n=n%i++<1?0:n);return n;}

0

Ruby + -rprime, 85 75 ไบต์

->n{*a=x=4
n.times{x+=1;!x.prime?&&a.none?{|k|(x+k+1).prime?}?a<<x:redo}
x}

ลองออนไลน์!

แลมบ์ดาคืนค่าองค์ประกอบที่ 0 ที่จัดทำดัชนีไว้

-10 ไบต์: การใช้งานredoและผู้ประกอบการที่ประกอบไปด้วยแทนที่จะเป็นloop...breakและห่วงโซ่เงื่อนไข

Ungolfed:

->n{
  *a=x=4                         # x is the most recent value: 4
                                 # a is the list of values so far: [4]
  n.times{                       # Repeat n times:
    x += 1                       # Increment x
    !x.prime? &&                 # If x is composite, and
      a.none?{|k|(x+k+1).prime?} #   for all k, a(n)+x+1 is composite,
      ? a<<x                     # Add x to a
      : redo                     # Else, restart the block (go to x+=1)
  }
  x                              # Return the most recent value
}


0

C (gcc) ,  140  138 ไบต์

ขอบคุณ @Jonathan Frech สำหรับการบันทึกสองไบต์!

c(n,i){for(i=1;++i<n;)i=n%i?i:n;i=i>n;}f(n){int s[n],k,j,i=0;for(*s=k=4;i++-n;i[s]=k)for(j=!++k;j-i;)2-c(k)-c(k-~s[j++])?j=!++k:f;n=n[s];}

0 การจัดทำดัชนี

ลองออนไลน์!


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