จำนวนเต็มบวกที่น้อยที่สุดซึ่งเป็น coprime สำหรับสองหน้าก่อนหน้าและยังไม่ปรากฎ a (1) = 1, a (2) = 2


10

คำนิยาม

  • สองจำนวนเต็มมีcoprimeถ้าพวกเขาร่วมกันไม่มีตัวหารร่วมกันในเชิงบวกอื่น ๆ 1กว่า
  • a(1) = 1
  • a(2) = 2
  • a(n)เป็นจำนวนเต็มบวกที่เล็กที่สุดซึ่งเป็น coprime ไปa(n-1)และและยังไม่ปรากฏสำหรับจำนวนเต็มa(n-2)n >= 3

งาน

  • ได้รับจำนวนเต็มบวกเอาท์พุทn / พิมพ์a(n)

ตัวอย่าง

  • a(11) = 6เพราะ6เป็น coprime กับสองคนก่อนหน้า (คือ11และ13) และ6ยังไม่เคยปรากฏมาก่อน

หมายเหตุ

  • โปรดทราบว่าลำดับไม่ได้ขึ้นหมายความว่าองค์ประกอบอาจมีขนาดเล็กกว่ารุ่นก่อน

รายละเอียด

  • คุณต้องใช้ 1 ดัชนี

Testcases

n      a(n)
1      1
2      2
3      3
4      5
5      4
6      7
7      9
8      8
9      11
10     13
11     6
12     17
13     19
14     10
15     21
16     23
17     16
18     15
19     29
20     14
100    139
1000   1355
10000  13387
100000 133361

เกณฑ์การให้คะแนน

  • เนื่องจาก coprime หมายความว่าตัวเลขสองตัวนั้นมีตัวหารเดียว ( 1) และ1มีจำนวนน้อยรหัสของคุณควรเล็กที่สุดเท่าที่จะเป็นไปได้ในแง่ของจำนวนไบต์

อ้างอิง


4
บรรดา "เหตุผล" สำหรับรหัสสั้น ...
หลุยส์ Mendo

1
ฉันสงสัยว่าทำไมสิ่งนี้จึงถูกลดระดับลง ไม่แน่นอนเพราะเหตุผลที่น่ากลัวใช่ไหม
Conor O'Brien

@Conor ไม่ใช่ฉัน จริง ๆ แล้วฉัน upvoted ฉันหวังว่าผู้คนจะเห็นทั้งเหตุผลและความคิดเห็นของฉันเป็นเรื่องตลก
หลุยส์เมนโด

3
ปัญหาเกี่ยวกับเหตุผล "ตลก" สำหรับโค้ดกอล์ฟคือฉันต้องอ่านมุขที่ไม่ดีซึ่งทอดสี่บรรทัดเพื่อค้นหาว่านี่คือรหัสกอล์ฟมาตรฐาน เป็นเพียงการปิดบังกฎของความท้าทายโดยไม่มีเหตุผลที่ดี
Martin Ender

1
@ ConorO'Brien เบราว์เซอร์บางตัวไม่แสดงชื่อเสมอ (แล้วมีแอพมือถือ) และโดยทั่วไปเราจะอธิบายการให้คะแนนในโพสต์นอกเหนือจากการใช้แท็กเนื่องจากแท็กเพียงอย่างเดียวไม่ได้มีความหมายอะไรกับคนที่ยังใหม่ ไปยังเว็บไซต์ ถึงแม้ว่าผมจะรู้สึกคุ้นเคยกับแท็กประเภทความท้าทายของเราผมไม่เคยอ่านพวกเขาจะคิดออกว่าเป็นความท้าทายที่จะทำแต้ม แต่พยายามที่จะพบว่าในร่างกายของความท้าทาย แท็กสำหรับการจัดหมวดหมู่การค้นหาและข้อมูลเฉพาะประเภทความท้าทายในแท็กวิกิ
Martin Ender

คำตอบ:


5

Python 3.5, 160 141 126 124 121 109 ไบต์

นี่เป็นการใช้งานคำจำกัดความของลำดับอย่างง่าย ยินดีต้อนรับคำแนะนำการเล่นกอล์ฟ

แก้ไข: -17 ไบต์ขอบคุณ Leaky Nun -9 ไบต์ขอบคุณ Peter Taylor -6 ไบต์ขอบคุณ Sp3000 และเปลี่ยนเป็น Python 3.5

import math;f=lambda n,r=[2,1],c=3:n<2and r[1]or(c in r)+math.gcd(c,r[0]*r[1])<2and f(n-1,[c]+r)or f(n,r,c+1)

Ungolfing:

import math
def f(n, r=[2,1], c=3):
    if n<2:
        return r[1]
    elif (c in r) + math.gcd(c,r[0]*r[1]) < 2:
        return f(n-1, [c]+r)
    else:
        return f(n, r, c+1)

สำหรับงูหลาม 3.5+, import mathแล้วควรจะสั้นกว่ากำหนดของคุณเองg=math.gcd gเพราะก่อนที่ 3.5 คุณสามารถทำสำหรับfrom fractions import* gcd
Sp3000

หากคุณเริ่มต้นc=3ภายในลูปคุณจะต้องทำเพียงครั้งเดียว โดยการนับของฉันคุณบันทึก 3 ตัวอักษร
Peter Taylor

นอกจากนี้ยังมีการประหยัดแบบ 2 ถ่านจากการสร้างอาเรย์ด้วยวิธีอื่น: คุณต้องใช้r=[c]+rมากกว่า+=แต่ดัชนีลบสามตัวจะกลายเป็นค่าบวก และมีการประหยัด 2 ถ่านเพิ่มเติมจากการเขียนใหม่เป็นแลมบ์ดาแม้ว่ามันจะเป็นการเปลี่ยนแปลงที่รุนแรงมากfrom fractions import*;F=lambda n,r=[2,1],c=3:n<2and r[1]or(c in r)+gcd(r[0]*r[1],c)<2and F(n-1,[c]+r)or F(n,r,c+1)และไม่จำเป็นต้องมีprintเพราะมันไม่ได้เป็นโปรแกรมเต็มรูปแบบอีกต่อไป
Peter Taylor

2

MATL , 28 27 ไบต์

2:i:"`@ym1MTF_)Zdqa+}@h]]G)

รหัสช้า แต่ให้ผลลัพธ์ที่ถูกต้อง

ลองออนไลน์! หรือตรวจสอบสิบกรณีแรก

การดัดแปลงโค้ดเพียงเล็กน้อยจะทำให้เกิดลำดับของพล็อต:

2:i:"`@ym1MTF_)Zdqa+}@h]]G:)XG

เห็นว่ามันเป็นศิลปะ ASCIIหรือเอาท์พุทกราฟิกในคอมไพเลอร์ออฟไลน์:

ป้อนคำอธิบายรูปภาพที่นี่

ป้อนคำอธิบายรูปภาพที่นี่

คำอธิบาย

2:         % Push [1 2] to initiallize the sequence
i:         % Input n. Push [1 2 ... n]
"          % For loop: repeat n times
  `        %   Do while loop
    @      %     Push iteration index, starting at 1. This is the candidate number
           %     to extend the sequence
    y      %     Duplicate vector containing the sequence so far
    m      %     Is member? Gives true if the candidate is in the sequence
    1M     %     Push candidate and vector again
    TF_)   %     Get last two elements of the vector
    Zd     %     GCD between the candidate and those two elements. Produces a
           %     two-element vector
    qa     %     True if any of the two results exceeds 1, meaning
           %     the candidate is not coprime with the latest two sequence values
    +      %     Add. This corresponds to logical "or" of the two conditions, namely
           %     whether the candidate is member of the sequence so far, and
           %     whether it is not coprime with the latest two. In either case
           %     the do...while must continue with a next iteration, to try a new
           %     candidate. Else the loop is exited, and the current candidate
           %     is the new value of the sequence
  }        %   Finally (execute when the loop is exited)
    @h     %     Push current candidate and concatenate to the sequence vector
  ]        %   End do...while
]          % End for
G)         % Get n-th value of the sequence. Implicitly display

1

C, 185 ไบต์

G(a,b){return a%b?G(b,a%b):b;}
i,j,k;f(n){int a[n+2];for(i=0;i++<n;){a[i]=i<3?i:0;for(j=2;!a[i];++j){for(k=i;--k;){if(a[k]==j)++j,k=i;}a[G(a[i-1],j)*G(a[i-2],j)<2?i:0]=j;}}return a[n];}

1

อันที่จริง , 38 37 35 33 วันที่ 31วันที่ 30 ไบต์

นี่เป็นการใช้งานคำจำกัดความของฟังก์ชันอย่างง่าย ยินดีต้อนรับคำแนะนำการเล่นกอล์ฟ ลองออนไลน์!

แก้ไข: -3 ไบต์ขอบคุณ Leaky Nun

2R#╗,;`1";2±╜tπg@╜í+Y"£╓╖`nD╜E

Ungolfing:

2R#╗    Push [1,2] and store it in register 0
,;      Take input and duplicate
`1      Start function, push 1
  "       Start string
  ;       Duplicate i
  2±╜t    Push (list in register 0)[-2:]
  πg      gcd(i, product of list[-2:])
  @╜í     Rotate the gcd and bring up i, check for i in list (0-based, -1 if not found)
  +Y      Add the gcd and the index, negate (1 if coprime and not found in list, else 0)
  "£      End string, turn into a function
╓       Push first (1) values where f(x) is truthy, starting with f(0)
╖`      Append result to the list in register 0, end function
n       Run function (input) times
D╜E     Return (final list)[n-1]

1
กองซ้อนกันมาก
Leun Nun

0

Haskell, 81 73 ไบต์

c l@(m:n:_)=m:c([x|x<-[1..],gcd(m*n)x<2,all(/=x)l]!!0:l)
((0:1:c[2,1])!!)

ตัวอย่างการใช้งาน: ->((0:1:c[2,1])!!) 1217

สร้างรายชื่อของทุกคนa(n)ที่เริ่มต้นด้วย0การแก้ไขดัชนี 1-based และและตามมาด้วย1 ยิงหัวของรายการอาร์กิวเมนต์มันฯตามด้วยโทร recursive ที่มีจำนวนต่อไปแล้วที่พอดี (ร่วมที่สำคัญไม่เคยเห็นมาก่อน) เพิ่มในด้านหน้าของ เลือกองค์ประกอบ th ของรายการนี้c[2,1]clln


0

R, 141 ไบต์

 f=Vectorize(function(n)ifelse(n>3,{c=3;a=f(n-1);b=f(n-2);d=f(4:n-3);while(!c%%which(!a%%1:a)[-1]||!c%%which(!b%%1:b)[-1]||c%in%d)c=c+1;c},n))

ungolfed

f=Vectorize( function(n)     #build a recursive function. Vectorize allows
    if(n>3) {                #the function to be called on vectors.
        c=3                  #Tests size. Builds some frequent variables.
        a=f(n-1)
        b=f(n-2)
        d=f(4:n-3)           #Should really golf this out, but its horribly slow.
        while(!c%%which(!a%%1:a)[-1]||!c%%which(!b%%1:b)[-1]||c%in%d)
              c=c+1          #If we are coprime and not already seen. add.
        c
     } else n)               #First three are 1,2,3.

0

Mathematica, 97 90 ไบต์

a@1=1;a@2=2;a@n_:=SelectFirst[Range[2n],GCD[a[n-1]a[n-2],#]<2&&!MemberQ[a/@Range[n-1],#]&]

ขึ้นอยู่กับการคาดเดาของฉันที่ ทั้งหมดa(n) < 2nn

เพื่อให้ได้ทำงานได้เร็วขึ้น, เพิ่มa@n=หลังจากที่เดิม:=เพื่อให้การทำงานไม่จำเป็นต้องคำนวณค่าก่อนหน้า

บันทึก 7 ไบต์ด้วย Sherlock9 (ถ้าเป็นเช่นgcd(a,b)=1นั้นgcd(ab,m) = gcd(a,m)*gcd(b,m))


ไม่ใช่การคาดเดาเนื่องจากเขียนไว้ในหน้า OEISว่า " ABS(a(n)-n) < n"
Leun Nun

@LeakyNun ขอบคุณ หน้า OEIS ลดลงจนไม่กี่ช่วงเวลาที่ผ่านมาและผมเป็นห่วงเกี่ยวกับ counterexample nเป็นไปได้สำหรับขนาดใหญ่

0

Pyth, 23 ไบต์

eu+Gf&-TGq1iT*F>2G1tQ]1

ชุดทดสอบ

การใช้งานที่ค่อนข้างตรงไปตรงมา แต่ด้วยเทคนิคการตีกอล์ฟ

eu+Gf&-TGq1iT*F>2G1tQ]1
 u                 tQ]1    Apply the following function input - 1 times,
                           where G is the current state (List of values so far)
  +G                       Add to G
    f             1        The first number, counting up from 1
      -TG                  That has not been seen so far
     &                     And where
               >2G         The most recent two numbers
             *F            Multiplied together
           iT              Gcd with the current number being checked
         q1                Equals 1
e                          Output the final element of the list.
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.