心电图序列的条款


13

介绍

EKG序列以1和2开头,因此规则是下一项是序列中尚未存在的最小正整数,并且其与最后一项的公因数大于1(它们不是互质数)。

第一项是:

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

之所以称为EKG,是因为其术语图与EKG非常相似。

它是OEIS中序列A064413

挑战

您必须编写一个函数,将整数n作为输入并输出序列中n个第一项中有多少大于n

由于序列的规则以第三项开头,因此输入整数必须大于或等于3。例如,给定输入10的输出是1因为第七项为12,而其他前十项都不超过10。

测试用例

3-> 1

10-> 1

100-> 9

1000-> 70

规则

  • 对于小于3的整数,该函数可能输出0或错误代码。
  • 没有其他特定规则,只有:编码高尔夫球,越短越好!

我们是否可以使用0-indexing,将其1作为序列的第0个术语,从而使之成为15第10个而不是5
毛茸茸的

@Shaggy我认为将其用作数学方法是公平的,但实际上它将改变测试用例的结果以及实际上是所要求的功能。因此,我认为您不应被允许这样做。抱歉。
大卫

Answers:


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个字节,感谢Jo King

{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

请您解释一下为什么要将输入加倍(用GE:)?
大卫

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

3

APL(Dyalog Unicode),39 字节SBCS

-2个字节归功于ngn,-1个字节归功于使用适当的条件检查。

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

在线尝试!


将其自己的左参数传递给操作数函数,因此不需要。同样,它不会与右边的东西绑定在一起,因为它以函数()开头,因此不需要
ngn

2

JavaScript,93 91 87字节

引发0或的溢出错误1,输出02

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
∇

在不到一分钟的时间内运行时间进行测试:

  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

1

Python 3,153字节

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

在线尝试!(警告:大约需要30秒才能评估)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.