EKGシーケンスの条件


13

前書き

EKGシーケンスは1と2で始まり、次の項はシーケンスにまだない最小の正の整数であり、最後の項との共通因子が1より大きい(それらは互いに素である)という規則です。

最初の用語は次のとおりです。

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

EKGと呼ばれるのは、用語のグラフがEKGに非常に似ているためです。

OEISシーケンスA064413です。

チャレンジ

入力として整数nを取り、シーケンスのn個の最初の項のうちn個より大きい数を出力する関数を作成する必要があります。

シーケンスのルールは3番目の項から始まるため、入力整数は3以上である必要があります。たとえば、入力10が与えられる1と、7番目の項が1210を超える他の10項のいずれも出力されないためです。

テストケース

3-> 1

10-> 1

100-> 9

1000-> 70

ルール

  • 3未満の整数の場合、関数は0またはエラーコードを出力する場合があります。
  • 他の特別なルールはありません:それはコードゴルフです、短いほど良いです!

1シーケンスの0番目の項である0 インデックスを使用して、たとえば、1510 番目の項を作成できます5か?
シャギー

@Shaggyこれを数学的な方法として使用するのは公平だと思いますが、実際にはテストケースの結果と実際に要求された関数自体を変更します。したがって、あなたはそうすることを許されるべきではないと思います。ごめんなさい。
デヴィッド・

oeis.org/A064413/graph-OEISはグラフを書くことができますか?きちんとした。
魔法のタコUr

回答:


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,]。ヘルパーリンクをn回呼び出すと、長さn+1シーケンスが生成されるため、0は実質的に無視されます。


6

Perl 6の66の 63 59 58バイト

ジョー・キングのおかげで-4バイト

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

オンラインでお試しください!

n = 1000のTIOが遅すぎる。


@JoKing 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

、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

なぜ入力を2倍にするのか説明できますGE:か?
デヴィッド・

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

3

APL(Dyalog Unicode)、39 バイト SBCS

ngnのおかげで-2バイト、適切な条件チェックを使用して-1バイト。

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

オンラインでお試しください!


独自の左引数をオペランド関数に渡すので、は必要ありません。また、関数()で始まるため、右側のものにバインドされないため、の必要はありません
ngn

2

JavaScript、93 91 87バイト

オーバーフローエラーをスロー0または1、出力0のために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)

オンラインで試す


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
∇

ここで実行時間を1分未満でテストします。

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

自然なタイプと範囲のチェックはありません...


1

Japt、23 21バイト

@_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.