Begriffe der EKG-Sequenz

13

Einführung

Die EKG-Sequenz beginnt mit 1 und 2, dann gilt die Regel, dass der nächste Term die kleinste positive ganze Zahl ist, die noch nicht in der Sequenz enthalten ist und deren gemeinsamer Faktor mit dem letzten Term größer als 1 ist (es handelt sich nicht um Coprimes).

Die ersten Begriffe sind:

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

Es wird EKG genannt, weil der Graph seiner Ausdrücke einem EKG ziemlich ähnlich ist.

Es ist die Sequenz A064413 im OEIS .

Herausforderung

Sie müssen eine Funktion schreiben, die eine ganze Zahl n als Eingabe verwendet und ausgibt, wie viele der n ersten Terme der Sequenz größer als n sind .

Da die Regel der Sequenz mit dem dritten Term beginnt, muss die Ganzzahl der Eingabe größer oder gleich 3 sein. Bei einer gegebenen Eingabe 10lautet die Ausgabe beispielsweise, 1dass der siebte Term 1210 ist und keiner der anderen ersten zehn Terme 10 überschreitet.

Testfälle

3 -> 1

10 -> 1

100 -> 9

1000 -> 70

Regeln

  • Für Ganzzahlen kleiner als 3 kann die Funktion 0 oder einen Fehlercode ausgeben.
  • Keine anderen Regeln außer: Es ist Code Golf, je kürzer desto besser!
David
quelle
Können wir die 0-Indizierung verwenden, wobei 1es sich um den 0. Term der Sequenz handelt und daher beispielsweise 15der 10. Term ist, anstatt 5?
Shaggy
@ Shaggy Ich denke, es ist fair, dies als mathematische Methode zu verwenden, aber tatsächlich wird es das Ergebnis von Testfällen und in der Tat die gestellte Funktion an sich ändern. Deshalb denke ich, dass Sie das nicht dürfen sollten. Es tut uns leid.
David
oeis.org/A064413/graph - OEIS kann Grafiken schreiben? Ordentlich.
Magic Octopus Urn

Antworten:

7

Jelly , 20 19 18 Bytes

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

Dies ist ein volles Programm.

Probieren Sie es online!

Wie es funktioniert

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.

Beachten Sie, dass die generierte Sequenz [1,0,2,4,6,3,9,12,8,10,5,15,] . Da der n fache Aufruf der Hilfsverbindung eine Folge der Länge n+1 , wird die 0 praktisch ignoriert.

Dennis
quelle
6

Perl 6 , 66 63 59 58 Bytes

-4 Bytes dank Jo King

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

Probieren Sie es online!

TIO für n = 1000 zu langsam.

nwellnhof
quelle
@JoKing Nachdem mir klar wurde, dass first &f,1..*das umgeschrieben werden kann +(1...&f), hat dein Junction-Trick doch geholfen.
Nwellnhof
4

JavaScript (ES6), 107 106 105 Bytes

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])

Probieren Sie es online!

Wie?

C

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

ein[2,1]ein[0]

k0

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

a.indexOf(k) ist gleich entweder:

  • -1ka
  • 0k
  • ich1

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

Arnauld
quelle
4

Haskell, 89 82 Bytes

Edit: -7 Bytes dank @ H.PWiz

f l=[n|n<-[3..],all(/=n)l,gcd(l!!0)n>1]!!0:l
g n=sum[1|x<-iterate f[2]!!(n-2),x>n]

Probieren Sie es online!

nimi
quelle
82 Bytes
H.PWiz
@ H.PWiz: ah, das ist schlau. Vielen Dank!
Nimi
4

Schale , 16 Bytes

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

Probieren Sie es online!

Erläuterung

#>¹↑¡§ḟȯ←⌋→`-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
Zgarb
quelle
3

MATL , 29 Bytes

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

Probieren Sie es online!

Erläuterung:

	#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
Giuseppe
quelle
Kannst du bitte erklären, warum du die Eingabe verdoppelst (mit GE:)?
david
2
ein(n)2nein(n)n2n=1000while
3

APL (Dyalog Unicode) , 39 Byte SBCS

-2 Bytes dank ngn, -1 Bytes durch korrekte bedingte Prüfung.

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

Probieren Sie es online!

voidhawk
quelle
Übergibt ein eigenes linkes Argument an die Operandenfunktion, sodass keine Notwendigkeit besteht . wird auch nicht mit dem Ding auf der rechten Seite verbunden, da es mit einer Funktion ( ) beginnt , es ist also nicht nötig .
22.
2

JavaScript, 93 91 87 Bytes

Löst einen Überlauffehler für 0oder 1, Ausgaben 0für aus 2.

n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)

Probieren Sie es online aus

Zottelig
quelle
2

APL (NARS), Zeichen 121, Bytes 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
∇

Test in weniger als einer Minute hier in der Laufzeit:

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

Natürlich gibt es keine Prüfung auf Typ und Reichweite ...

RosLuP
quelle
1

Japt, 23 21 Bytes

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

Versuch es

@_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
Zottelig
quelle
1

Python 3 , 153 Bytes

import math
def f(n):
 l=[1];c=0
 for _ in range(n-1):l+=[min(*range(2,n*4),key=lambda x:n*8if x in l or math.gcd(x,l[-1])<2else x)];c+=l[-1]>n
 return c

Probieren Sie es online! (Warnung: Die Auswertung dauert ca. 30 Sekunden.)

Schwarze Eule Kai
quelle