整数平方根的序列


17

让我们定义一个整数平方根的序列。首先,a(1)=1。然后,a(n)是之前从未见过的最小正整数,因此

sqrt(a(n) + sqrt(a(n-1) + sqrt(... + sqrt(a(1)))))

是一个整数。一些例子:

a(2)是3,因为它是最小的整数,因此 sqrt(a(2) + sqrt(a(1))) = sqrt(a(2) + 1)整数,并且之前的序列中未出现3。

a(3)是2,因为它是最小的整数,即sqrt(a(3) + sqrt(a(2) + sqrt(a(1)))) = sqrt(a(3) + 2)整数,并且之前的序列中未出现2。

a(4)是7,因为它sqrt(a(4) + 2)是整数。我们不可能有a(4)= 2,因为在我们的序列中已经出现了2个。

编写一个给定参数n的程序或函数,其返回数字序列a(1)至a(n)。

该序列以1,3,2,7,6,13,5,...开始。

该序列的来源来自此Math.SE问题


序列中前1000个元素的图:

情节



1
@ Mr.Xcoder真有趣!
orlp

@ Mr.Xcoder是的,我同意这太糟糕了,您不能只复制粘贴公式...
Erik the Outgolfer

2
@EriktheOutgolfer编号。当您获得n作为输入时,您应该返回或打印a(1)到a(n)的列表。换句话说,序列中的前n个数字。没有“索引”。
orlp

1
对于很大的输入,是否可以接受由浮点错误引起的错误?
Zgarb

Answers:



3

Haskell103 87字节

效率极低,但不依赖浮点运算。这a(x) = sqrt(f(x)+a(x-1))是一个帮助程序序列,可简化计算。

a 0=0
a x=[k|k<-[1..],m<-[k^2-a(x-1)],m>0,notElem m$f<$>[1..x-1]]!!0
f x=(a x)^2-a(x-1)

在线尝试!



3

MATL30 27字节

lXHiq:"`@ymH@+X^1\+}8MXHx@h

在线尝试!或查看图形显示(需要一段时间;输入超过大约会超时60)。

说明

l          % Push 1. This is the array that holds the sequence, initialized to
           % a single term. Will be extended with subsequent terms
XH         % Copy into clipboard H, which holds the latest result of the 
           % "accumulated" square root
iq:"       % Input n. Do the following n-1 times
  `        %   Do...while
    @      %     Push interaton index k, starting at 1. This is the candidate
           %     to being the next term of the sequence
    y      %     Push copy of array of terms found so far
    m      %     Ismbmer? True if k is in the array
    H      %     Push accumulated root
    @+     %     Add k
    X^     %     Square root
    1\     %     Modulo 1. This gives 0 if k gives an integer square root
    +      %     Add. Gives nonzero if k is in the array or doesn't give an
           %     integer square root; that is, if k is invalid.
           %   The body of the do...while loop ends here. If the top of the
           %   stack is nonzero a new iteration will be run. If it is zero that
           %   means that the current k is a new term of the sequence
  }        %   Finally: this is executed after the last iteration, right before
           %   the loop is exited
    8M     %     Push latest result of the square root
    XH     %     Copy in clipboard K
    x      %     Delete
    @      %     Push current k
    h      %     Append to the array
           % End do...while (implicit)
           % Display (implicit)

3

Mathematica,104个字节

(s=f={i=1};Do[t=1;While[!IntegerQ[d=Sqrt[t+s[[i]]]]||!f~FreeQ~t,t++];f~(A=AppendTo)~t;s~A~d;i++,#-1];f)&  


在线尝试!

平方根的顺序也非常有趣...
并输出类似的模式

1,2,2,3,3,4,3,5,3,6,4,4,5,4,6,5,5,6,6,7,4,7,5,7,6, 8,4,8,5,8,6,9,5,9,6,10,5,10,6,11,5,11,6,12,6,13,6,14,7,7, 8,7,9,7,10,7,11,7,12,7,13,7,14,8,8,9,8,10 ...

在此处输入图片说明

这也是主要顺序的差异

在此处输入图片说明



2

JavaScript(ES7),89 82 77 76字节

i=>(g=k=>(s=(++n+k)**.5)%1||u[n]?g(k):i--?[u[n]=n,...g(s,n=0)]:[])(n=0,u=[])

演示版

格式化和评论

i => (                             // given i = number of terms to compute
  u = [],                          // u = array of encountered values
  g = p =>                         // g = recursive function taking p = previous square root
    (s = (++n + p) ** .5) % 1      // increment n; if n + p is not a perfect square,
    || u[n] ?                      // or n was already used:
      g(p)                         //   do a recursive call with p unchanged
    :                              // else:
      i-- ?                        //   if there are other terms to compute:
        [u[n] = n, ...g(s, n = 0)] //     append n, set u[n] and call g() with p = s, n = 0
      :                            //   else:
        []                         //     stop recursion
  )(n = 0)                         // initial call to g() with n = p = 0

2

[R 138个 105 99字节

function(n){for(i in 1:n){j=1
while(Reduce(function(x,y)(y+x)^.5,g<-c(T,j))%%1|j%in%T)j=j+1
T=g}
T}

在线尝试!

-33字节,在while循环中使用Tfeld的巧妙sqrt()%%1技巧

-6字节使用T代替F

原始答案,138个字节:

function(n,l={}){g=function(L)Reduce(function(x,y)(y+x)^.5,L,0)
for(i in 1:n){T=1
while(g(c(l,T))!=g(c(l,T))%/%1|T%in%l)T=T+1
l=c(l,T)}
l}

在线尝试!


2

外壳,21字节

!¡oḟȯΛ±sFo√+Som:`-N;1

在线尝试!

怎么样?

!¡oḟȯΛ±sFo√+Som:`-N;1    Function that generates a list of prefixes of the sequence and indexes into it
                   ;1    The literal list [1]
 ¡                       Iterate the following function, collecting values in a list
  oḟȯΛ±sFo√+Som:`-N        This function takes a prefix of the sequence, l, and returns the next prefix.
                `-N      Get all the natural numbers that are not in l.
            Som:         Append l in front each of these numbers, generates all possible prefixes.
    ȯΛ±sFo√+               This predicate tests if sqrt(a(n) + sqrt(a(n-1) + sqrt(... + sqrt(a(1))))) is an integer.
        F                Fold from the left
         o√+             the composition of square root and plus
       s                 Convert to string
    ȯΛ±                  Are all the characters digits, (no '.')
  oḟ                     Find the first list in the list of possible prefixes that satisfies the above predicate
!                        Index into the list
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.