尚未使用的对


21

让我们定义一个正整数序列。我们将偶数上的顺序定义为上一项的两倍。序列的奇数索引将是序列中尚未出现的最小正整数。

这是前几个术语。

1,2,3,6,4,8,5,10,7,14,9,18,11,22,12,24,13,26,15,30

您也可以将其视为级联对(n,2n)的列表,其中n是到目前为止最少未使用的正整数。

任务

给定数字n作为输入,请按此顺序计算第n个项。

这是因此您应该以最小化以字节为单位的源代码大小为目标。

OEIS A036552


序列的奇数索引将是序列中尚未出现的最小正整数的事实不相关吧?
亚当

1
另外,什么
亚当

@Adám不,不是这样。我不确定是什么使您产生这种印象,也许我措辞不佳。
小麦巫师

1
@Adám认为序列的另一种方法是,它由级联对组成,(n,2n)每个数字仅出现一次。在遵守后一个约束的同时,将每对选择为尽可能最小。
马丁·恩德

3
该系列中奇数元素的2阶估值始终是偶数。对某人可能有用。
CalculatorFeline

Answers:


11

Haskell,40个字节

l(a:r)=a:2*a:l[x|x<-r,x/=2*a]
(l[1..]!!)

从零开始。 l从剩余整数的惰性列表逐步构建序列。


7

JavaScript(ES6),92 82 69 67 65字节

n=>(F=i=>i^n?F(a[b=i&1?2*b:(g=k=>a[k]?g(k+1):k)(1)]=-~i):b)(a={})

怎么样?

我们跟踪:

  • 最后插入的值b
  • 查找表a中以前遇到的所有值。

在内部,我们使用基于0的索引i。因此,奇数甚至偶数的行为都被反转了:

  • 在奇数位置,下一个值就是2 * b

  • 在偶数位置,我们使用递归函数g()和查找表a来标识最小的匹配值:

    (g = k => a[k] ? g(k + 1) : k)(1)

为了节省一些字节,被初始化为{}而不是0。这迫使我们使用:

  • i^nin比较,因为({}) ^ n === n({}) - n求和NaN
  • -~i增加i,因为({}) + 1会生成一个字符串。

演示版



5

Python 3中80个 72 69字节

-7个字节,感谢Xcoder先生

f=lambda n:n and[f(n-1)*2,min({*range(n+1)}-{*map(f,range(n))})][n%2]

在线尝试!


1
您可以repace set(...)用`{* ...}为78个字节
Xcoder先生

@Zacharý您是否屈服于我的评论?如果是这样,可以使用Python 3中的set {*...}代替set(...)
Xcoder先生17年

我不加思索地评论,过了一会儿,我意识到{...for...in...}应该再见了。
扎卡里

实际上,它将节省4个字节,因为您使用了两次
Xcoder先生,2017年





3

PHP,56字节

while($$n=$i++<$argn)for($n*=2;$i&$$k&&$n=++$k;);echo$n;

PHP,75 72字节

for($a=range(1,$argn);$i++<$argn;)$a[~-$n=$i&1?min($a):2*$n]=INF;echo$n;

在线尝试


3

05AB1E16 15 14字节

1个索引。
使用以下事实:序列中奇数索引处的元素的二进制表示形式以偶数个零结尾:A003159

Lʒb1¡`gÈ}€x¹<è

在线尝试!

说明

L                 # range [1 ... input]
 ʒ      }         # filter, keep only elements whose
  b               # ... binary representation
   1¡             # ... split at 1's
     `gÈ          # ... ends in an even length run
         €x       # push a doubled copy of each element in place
           ¹<è    # get the element at index (input-1)

3

Python 2中59个 51 49字节

f=lambda n,k=2:2/n%-3*(1-k)or f(n+~(k&-k)%-3,k+1)

在线尝试!

背景

每个正整数n都可以唯一地表示为n = 2 o(n) c(n),其中c(n)是奇数。

⟨a ñN> 0是从挑战规范的序列。

我们声称,对于所有正整数no(a 2n-1是偶数。因为o(a 2n)= o(2a 2n-1)= o(a 2n-1)+1,所以这等同于声称o(a 2n总是奇数。

假设该要求是错误的,令2m-1为序列的第一个奇数索引,以使o(a 2m-1为奇数。注意,这使2m成为序列的第一个偶数索引,从而o(a 2m-1是偶数。

o(a2m-1) is odd and 0 is even, so a2m-1 is divisible by 2. By definition, a2m-1 is the smallest positive integer not yet appearing in the sequence, meaning that a2m-1/2 must have appeared before. Let k be the (first) index of a2m-1/2 in a.

Since o(ak) = o(a2m-1/2) = o(a2m-1) - 1 is even, the minimality of n implies that k is odd. In turn, this means that ak+1 = 2ak = a2m-1, contradicting the definition of a2m-1.

How it works

yet to come


3

R, 70 69 65 bytes

function(n){for(i in 2*1:n)F[i-1:0]=which(!1:n%in%F)[1]*1:2
F[n]}

Try it online!

An anonymous function that takes one argument. F defaults to FALSE or 0 so that the algorithm correctly assesses that no positive integers are in the sequence yet.

The algorithm generates the pairs in a for loop in the following manner (where i goes from 2 to 2n by 2):

           which(!1:n%in%l)[1]     # the missing value
                              *1:2 # keep one copy the same and double the next
l[i-1:0]=                         # store into l at the indices i-1 and i


2

Perl 6, 50 bytes

{(1,{@_%2??2*@_[*-1]!!first *∉@_,1..*}...*)[$_]}

Try it online!

  • 1, { ... } ... * is a lazily-generated infinite sequence where each term after the first is provided by the brace-delimited code block. Since the block references the @_ array, it receives the entire current sequence in that array.
  • If the current number of elements is odd (@_ % 2), we're generating an even-indexed element, so the next element is double the last element we have so far: 2 * @_[*-1].
  • Otherwise, we get the first positive integer that does not yet appear in the sequence: first * ∉ @_, 1..*.
  • $_ is the argument to the outer function. It indexes into the infinite sequence, giving the function's return value.

1

Mathematica, 82 bytes

(s={};a=1;f=#;While[f>0,If[s~FreeQ~a,s~AppendTo~{a,2a}];a++;f--];Flatten[s][[#]])&

Try it online!


58 bytes in Mathematica (although I should probably just post a separate answer since the idea is pretty different).
notjagan

Did you copy that from OEIS link?
J42161217

I modified it to fit the task and to be golfed more, but it more or less is the same as the OEIS link.
notjagan

1
@not post a new answer if you want and credit the author
J42161217


1

C# (Visual C# Interactive Compiler), 82 bytes

x=>{int y=1;for(var s="";x>2;x-=2)for(s+=2*y+":";s.Contains(++y+""););return x*y;}

Try it online!

-6 bytes thanks to @ASCIIOnly!


C#8 may be too new to be common on online interpreters for now, added to the fact that csi is a Mono thing so you'd have to wait for Mono to implement it and add it to a stable build (if it hasn't already)
ASCII-only

sadly it isn't easy to check for this in C#
ASCII-only

Use this to start? But yeah, doesn't seem like a straightforward thing. docs.microsoft.com/en-us/dotnet/api/…
dana

1
86? - don't think the :s are needed since it will be the largest number in the list
ASCII-only

Also 2.0 => 2f
dana

0

Clojure, 102 bytes

#(nth(loop[l[0 1 2 3]i %](if(= i 0)l(recur(conj l(*(last l)2)(nth(remove(set l)(range))0))(dec i))))%)

Iterates n times to build up the sequence and returns the nth item, 1-indexed.


0

Ruby, 60 bytes

->n,*a{eval"v+=1while a[v];a[v]=a[2*v]=v+v*n%=2;"*(n/2+v=1)}

0-indexed. We loop n/2+1 times, generating two values each time and storing them by populating an array at their indices. v+v*n%2 gives the output, either v or v*2 depending on the parity of n.


0

Ruby, 49 bytes

->n{*s=t=0;s|[t+=1]==s||s<<t<<t*2until s[n];s[n]}

Start with [0], add pairs to the array until we have at least n+1 elements, then take the n+1th (1-based)

Try it online!


0

JavaScript (ES6), 60 65 bytes

An iterative solution.

n=>eval("for(s={},i=0;n;)s[++i]?0:(s[i+i]=--n)?--n?0:i+i:i")

Less golfed

n=>{
  s = {}; //hashtable for used values
  for(i=0; n; )
  {
    if ( ! s[++i] )
    {
      s[i*2] = 1; // remember i*2 is already used
      if (--n)
        if (--n)
          0;
        else
          result = i*2;
      else
        result = i;
    }
  }
  return result;  
}

Test

F=
n=>eval("for(s={},i=0;n;)s[++i]?0:(s[i+i]=--n)?--n?0:i+i:i")

for (a=1; a < 50; a++)
  console.log(a,F(a))


0

Jelly, 13 12 10 bytes

ḤRọ2ḂĠZFị@

This uses the observation from my Python answer.

Try it online!

How it works

ḤRọ2ḂĠZFị@  Main link. Argument: n

Ḥ           Unhalve; yield 2n.
 R          Range; yield [1, ... , 2n].
  ọ2        Compute the order of 2 in the factorization of each k in [1, ..., 2n].
    Ḃ       Bit; compute the parity of each order.
     G      Group the indices [1, ..., 2n] by the corresponding values.
      Z     Zip/transpose the resulting 2D array, interleaving the indices of 0
            with the indices of 1, as a list of pairs.
       F    Flatten. This yields a prefix of the sequence.
        ị@  Take the item at index n.
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.