ข้อกำหนดของลำดับ EKG


13

บทนำ

ลำดับ EKG เริ่มต้นด้วย 1 และ 2 จากนั้นกฎคือเทอมถัดไปคือเลขจำนวนเต็มบวกที่เล็กที่สุดที่ไม่ได้อยู่ในลำดับและมีปัจจัยร่วมกับคำสุดท้ายมากกว่า 1 (ไม่ใช่โคพริม์ม)

เงื่อนไขแรกคือ:

1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, ...

มันเรียกว่า EKG เพราะกราฟของคำศัพท์ค่อนข้างคล้ายกับ EKG

มันเป็นลำดับ A064413 ใน OEIS

ท้าทาย

คุณต้องเขียนฟังก์ชั่นซึ่งจะนำจำนวนเต็มnเป็นอินพุตและเอาต์พุตหลายวิธีของnแรกแง่ของลำดับมากกว่าn

เมื่อกฎของลำดับเริ่มต้นด้วยคำที่สามจำนวนเต็มอินพุทจะต้องมากกว่าหรือเท่ากับ 3 ตัวอย่างเช่นการป้อนข้อมูลที่กำหนด10เอาท์พุทเป็น1เพราะเทอมที่ 7 คือ12และไม่มีเทอมแรกที่เกิน 10 อีก

กรณีทดสอบ

3 -> 1

10 -> 1

100 -> 9

1,000 -> 70

กฎระเบียบ

  • สำหรับจำนวนเต็มต่ำกว่า 3 ฟังก์ชั่นอาจส่งออก 0 หรือรหัสข้อผิดพลาด
  • ไม่มีกฎเฉพาะอื่น ๆ ยกเว้น: มันเป็นรหัสกอล์ฟยิ่งสั้นยิ่งดี!

เราสามารถใช้การจัดทำดัชนี 0 โดย1เป็นคำที่ 0 ของลำดับและทำเช่นนั้น15สำหรับคำที่ 10 แทนได้5หรือไม่
Shaggy

@Shaggy ฉันคิดว่ามันยุติธรรมที่จะใช้สิ่งนี้เป็นวิธีการทางคณิตศาสตร์ แต่จริง ๆ แล้วมันจะเปลี่ยนผลลัพธ์ของกรณีทดสอบและแน่นอนฟังก์ชั่นที่ถามในตัวเอง ดังนั้นฉันคิดว่าคุณไม่ควรได้รับอนุญาตให้ทำเช่นนั้น ขอโทษ
david

oeis.org/A064413/graph - OEIS สามารถเขียนกราฟได้หรือไม่ เรียบร้อย
Magic Octopus Urn

คำตอบ:


7

เยลลี่ , 20 19 18 ไบต์

S‘gṪ’ɗƇḟ¹Ṃṭ
1Ç¡>¹S

นี่เป็นโปรแกรมเต็ม

ลองออนไลน์!

มันทำงานอย่างไร

1Ç¡>¹S       Main link. Argument: n (integer)

1            Set the return value to 1.
 Ç¡          Call the helper link n times.
   >¹        Compare the elements of the result with n.
     S       Take the sum, counting elements larger than n.


S‘gṪ’ɗƇḟ¹Ṃṭ  Helper link. Argument: A (array or 1)

S            Take the sum of A.
 ‘           Increment; add 1.
     ɗƇ      Drei comb; keep only elements k of [1, ..., sum(A)+1] for which the
             three links to the left return a truthy value.
  g              Take the GCD of k and all elements of A.
   Ṫ             Tail; extract the last GCD.
    ’            Decrement the result, mapping 1 to 0.
       ḟ¹    Filterfalse; remove the elements that occur in A.
         Ṃ   Take the minimum.
          ṭ  Tack; append the minimum to A.

[1,0,2,4,6,3,9,12,8,10,5,15,]nn+10


6

Perl 6 , 66 63 59 58 ไบต์

-4 ไบต์ขอบคุณ Jo King

{sum (1,2,{+(1...all *gcd@_[*-1]>1,*∉@_)}...*)[^$_]X>$_}

ลองออนไลน์!

ช้าเกินไปสำหรับ TIO สำหรับ n = 1,000


@ โจกิ้งหลังจากที่ฉันรู้ว่าfirst &f,1..*สามารถเขียนใหม่ได้เป็น+(1...&f)กลอุบายทางแยกของคุณก็ช่วยได้ทั้งหมด
nwellnhof

4

JavaScript (ES6), 107 106 105 ไบต์

f=(n,a=[2,1],k=3)=>a[n-1]?0:a.indexOf(k)+(C=(a,b)=>b?C(b,a%b):a>1)(k,a[0])?f(n,a,k+1):(k>n)+f(n,[k,...a])

ลองออนไลน์!

อย่างไร?

C

C = (a, b) => b ? C(b, a % b) : a > 1

a[2,1]a[0]

k0

a.indexOf(k) + C(k, a[0])

a.indexOf(k) เท่ากับ:

  • 1ka
  • 0k
  • i1

a.indexOf(k) + C(k, a[0])0kak1+true=0



4

Husk , 16 ไบต์

#>¹↑¡§ḟȯ←⌋→`-Nḣ2

ลองออนไลน์!

คำอธิบาย

#>¹↑¡§ḟȯ←⌋→`-Nḣ2  Implicit input, say n=10
              ḣ2  Range to 2: [1,2]
    ¡             Construct an infinite list, adding new elements using this function:
                   Argument is list of numbers found so far, say L=[1,2,4]
             N     Natural numbers: [1,2,3,4,5,6,7...
           `-      Remove elements of L: K=[3,5,6,7...
      ḟ            Find first element of K that satisfies this:
                    Argument is a number in K, say 6
     §    →         Last element of L: 4
         ⌋          GCD: 2
       ȯ←           Decrement: 1
                    Implicitly: is it nonzero? Yes, so 6 is good.
                  Result is the EKG sequence: [1,2,4,6,3,9,12...
   ↑              Take the first n elements: [1,2,4,6,3,9,12,8,10,5]
#                 Count the number of those
 >¹               that are larger than n: 1

3

MATL , 29 ไบต์

qq:2:w"GE:yX-y0)yZdqg)1)h]G>z

ลองออนไลน์!

คำอธิบาย:

	#implicit input, n, say 10
qq:	#push 1:8
2:	#push [1 2]. Stack: {[1 .. 8], [1 2]}
w	#swap top two elements on stack
"	#begin for loop (do the following n-2 times):
 GE:	#push 1...20. Stack: {[1 2], [1..20]}
 y	#copy from below. Stack:{[1 2], [1..20], [1 2]}
 X-	#set difference. Stack: {[1 2], [3..20]}
 y0)	#copy last element from below. Stack:{[1 2], [3..20], 2}
 yZd	#copy from below and elementwise GCD. Stack:{[1 2], [3..20],[1,2,etc.]}
 qg)	#select those with gcd greater than 1. Stack:{[1 2], [4,6,etc.]}
 1)	#take first. Stack:{[1 2], 4}
 h	#horizontally concatenate. Stack:{[1 2 4]}
 ]	#end of for loop
G>z	#count those greater than input
	#implicit output of result

คุณช่วยอธิบายได้GE:ไหม
david

2
a(n)2na(n)n2n=1000while

3

APL (Dyalog Unicode) , 39 ไบต์SBCS

-2 ไบต์ด้วย ngn, -1 ไบต์โดยใช้การตรวจสอบตามเงื่อนไขที่เหมาะสม

{+/⍵<⍵⍴3{(1=⍺∨⊃⌽⍵)∨⍺∊⍵:⍵∇⍨⍺+1⋄⍵,⍺}⍣⍵⍳2}

ลองออนไลน์!


ผ่านการโต้แย้งทางด้านซ้ายของตัวเองเพื่อฟังก์ชั่นตัวถูกดำเนินการเพื่อให้มีความจำเป็นในการ ยังจะไม่ผูกพันกับสิ่งที่อยู่ด้านขวาในขณะที่มันเริ่มต้นด้วยฟังก์ชั่น ( ) จึงไม่มีความจำเป็นในการ
ngn


2

APL (NARS), ตัวอักษร 121, ไบต์ 242

∇r←a w;i;j;v
r←w⋄→0×⍳w≤2⋄i←2⋄r←⍳2⋄v←1,1,(2×w)⍴0
j←¯1+v⍳0
j+←1⋄→3×⍳1=j⊃v⋄→3×⍳∼1<j∨i⊃r⋄r←r,j⋄i+←1⋄v[j]←1⋄→2×⍳w>i
r←+/w<r
∇

ทดสอบในเวลาน้อยกว่าหนึ่งนาทีที่นี่ในเวลาทำงาน:

  a¨3 10 100 1000 2000
1 1 9 70 128 

เป็นธรรมชาติไม่มีการตรวจสอบประเภทและช่วง ...


1

Japt 23 23ไบต์

@_jX ªAøZ}f}gA=ì)Aè>U

ลองมัน

@_jX ªAøZ}f}gA=ì)Aè>U
                          :Implicit input of integer U
             A            :10
               ì          :Digit array
              =           :Reassign to A
@          }g             :While the length of A < U+1, take the last element as X,
                          :pass it through the following function & push the result to A
 _       }f               :  Find the first integer Z >= 0 that returns falsey
  jX                      :    Is Z co-prime with X?
     ª                    :    OR
      AøZ                 :    Does A contain Z?
                )         :End loop
                 Aè>U     :Count the elements in A that are greater than U

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