整数平方根のシーケンス


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)は、整数である最小の整数であるためsqrt(a(3) + sqrt(a(2) + sqrt(a(1)))) = sqrt(a(3) + 2)、2であり、2は前のシーケンスでは発生していません。

sqrt(a(4) + 2)整数であるため、a(4)は7 です。シーケンスに2がすでに発生しているため、a(4)= 2を使用できませんでした。

パラメーターnを指定したプログラムまたは関数を作成すると、一連の数値a(1)からa(n)が返されます。

シーケンスは1,3,2,7,6,13,5、...から始まります。

このシーケンスのソースは、このMath.SE質問からのものです


シーケンスの最初の1000個の要素のプロット:

プロット



1
@ Mr.Xcoderそれはただそれを面白くするだけです!
-orlp

@ Mr.Xcoderうん、私はあなただけ...数式をコピー&ペーストすることはできません、それはとても悪いです同意
エリックOutgolfer

2
@EriktheOutgolferいいえ。入力としてnを取得した場合、a(1)からa(n)のリストを返すか、印刷する必要があります。つまり、シーケンスの最初のn個の数字。「インデックス付け」はありません。
orlp

1
浮動小数点の不正確さが原因のエラーは、非常に大きな入力に対して許容できますか?
-Zgarb

回答:



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

R138の 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}

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

whileループでTfeldの巧妙なsqrt()%%1トリックを使用した-33バイト

Fの代わりにTを使用した-6バイト

元の回答、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
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.