「初期の鳥」の正方形


15

定義

正の整数の正方形のシーケンスを取得し、それらを数字の文字列(つまり149162536496481100...)に連結する場合、「初期の鳥」の正方形は、自然な位置の前にこの文字列で見つかるものです。

たとえば、7 2(数値49)は、文字列のオフセット2にありますが、自然な位置はオフセット10です。したがって、7は最初の「早い鳥」の正方形です。

「初期の鳥」の正方形と見なされるには、正方形内のすべての数字が自然な位置の開始前に発生する必要があることに注意してください。自然な位置と部分的に重複する一致はカウントされません。

a(n)k 2が「初期の鳥」の正方形であるようなn番目の正の整数k です。

仕事

正の整数を指定するとn、出力されますa(n)

1ベースまたは0ベースのインデックスを使用できますが、0ベースのインデックスを使用する場合は、回答の中でそう言ってください。

ソリューションは、少なくともa(53)(または、0ベースのインデックス付けを使用している場合)と同等の高さを処理できる必要がありますa(52)

テストケース

n     a(n)
1     7
2     8
3     21
4     25
5     46
6     97
7     129
8     161
9     196
10    221
...
13    277
...
50    30015
51    35000
52    39250
53    46111

参照資料


テストケースのテーブルはベース0または1を使用していますか?
idrougge

1
nシーケンスの最初の要素を出力できますか?OP次第ですが、多くの人がそれを許可しています。
ハイパーニュートリノ

@idrouggeテストケースは1ベースです。
ジェームズホルダーネス

@HyperNeutrinoすべての回答に対して一貫した結果セットを取得したいので、単一の値を返してくださいa(n)
ジェームズホルダーネス

回答:


5

05AB1E10 9バイト

Adnanのおかげで1バイト節約されました。

µNL<nJNnå

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

説明

µ           # loop until counter equals the input
 NL         # push the range [1 ... iteration_no]
   <        # decrement each
    n       # square each
     J      # join to string
      Nnå   # is iteration_no in the string?
            # if true, increase counter

あなたは出て残すことができます½欠けているときなどは自動的にループに追加されます。
アドナン

@Adnan:はい。私が電車に飛び乗る直前に(または、遅れない場合は行こうとしていた)この挑戦に気づいたので、私はそれを完全に逃しました。ありがとう:)
エミグナ

7

JavaScript(ES6)、51 49 45バイト

1インデックス。

f=(n,s=k='')=>n?f(n-!!s.match(++k*k),s+k*k):k

デモ

書式設定およびコメント化

f = (                         // f = recursive function taking:
  n,                          //   n = input
  s = k = ''                  //   s = string of concatenated squares, k = counter
) =>                          //
  n ?                         // if we haven't reached the n-th term yet:
    f(                        //   do a recursive call with:
      n - !!s.match(++k * k), //     n decremented if k² is an early bird square
      s + k * k               //     s updated
    )                         //   end of recursive call
  :                           // else:
    k                         //   return k

非再帰バージョン、53バイト

これは、エンジンスタックサイズに依存しません。

n=>{for(k=s='';n-=!!(s+=k*k).match(++k*k););return k}

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


6

Pyth、12バイト

e.f/jk^R2Z`*

ここで試してみてください!

使い方

ef / jk ^ R2Z` *〜完全なプログラム。Qを入力とします。

 .f〜真の結果を持つ最初のQ個の正の整数。変数Zを使用します。
      ^ R2Z〜[0、Z)の範囲の各整数を二乗します。
    jk〜単一の文字列に連結します。
   /〜の発生をカウント...
          `*〜Zの2乗の文字列表現。
               偽の場合は0、真の場合は1以上をもたらします。
e〜最後の要素(Qthの真の整数)を取得します。暗黙的に出力します。


4

APL(Dyalog)53 42バイト

{{0<+/(⍕×⍨⍵+1)⍷' '~⍨⍕×⍨⍳⍵:⍵+1⋄∇⍵+1}⍣⍵⊢0}

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

どうやって?

-の出現を見つける

⍕×⍨⍵+1 -文字列化された正方形 x+1

⍕×⍨⍳⍵ -文字列化された正方形の範囲 x

' '~⍨ -スペースなし

+/ -合計

0<-合計が正の場合(発生が存在する場合)、それx+1以外の場合、

∇⍵+1-で再帰しx+1ます。

⍣⍵- n時間を適用します。


3

Haskell、73バイト

import Data.List
([n|n<-[7..],isInfixOf(g n)$g=<<[1..n-1]]!!)
g=show.(^2)

オンラインでお試しください!ゼロインデックス。

説明

補助剤:

import Data.List -- import needed for isInfixOf
g=show.(^2)      -- function short cut to square an int and get the string representation

メイン機能:

(                                 !!) -- Index into the infinite sequence
 [n|n<-[7..],                    ]    -- of all numbers n greater equal 7
      isInfixOf(g n)$                 -- whose square appears in the string
                     g=<<[1..n-1]     -- of all squares from 1 up to n-1 concatenated.

2

ゼリー13 11バイト

R²DµṪẇF
Ç#Ṫ

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

あるいは、これはnシーケンスの最初の値を印刷する10バイトのソリューションです。オンラインで試してください!


笑これに私を打ちました。私はあなたの解決策とまったく同じでした(ゴルフの後):P
HyperNeutrino

@HyperNeutrino残念ながら間違っているようです。
user202729

まあ、本当に?それは残念です:( 編集ああ正しいことnfind:(((
HyperNeutrino

@HyperNeutrino問題ありません、stdinからの読み取りは動作します。
user202729


2

ゼリー、11バイト

Ḷ²DFɓ²ẇ
Ç#Ṫ

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

user202729のソリューションの代替。

使い方

C#Ṫ ~ Main Link.

Ç#  ~ First N positive integers with truthy results.
  Ṫ ~ Tail. Take the last one.

-----------------------------------------------------------

Ḷ²DFɓ²ẇ ~ Helper link. This is the filtering condition.

Ḷ       ~ Lowered range. Yields {x | x ∊ Z and x ∊ [0, N)}.
 ²      ~ Square each.
  D     ~ Convert each to decimal (this gets the list of digits).
   F    ~ Flatten.
    ɓ   ~ Starts a new monadic chain with swapped arguments.
     ²  ~ N²; Yields N squared.
      ẇ ~ Is ^ sublist of ^^^?

うわー、自動文字列化があります。
user202729

2

アリス、32バイト

/
\io/&wd.*\@! d ? ~ ? F $ /WKdt

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

Ordinalモードのそのストレッチの無駄なレイアウトは本当に私を悩ませていますが、そこにいくつかのバイトを保存しようとするすべてがより長く出てきます...

説明

/
\io/...@...

いつもの小数I / Oフレームワーク、oおよび@少し珍しい位置インチ プログラムの要点は次のとおりです。

&w    Push the current IP address to the return address stack n times.
      This gives us an easy way to write a loop which repeats until we
      explicitly decrement the loop counter n times.

  d     Push the stack depth, which acts as our running iterator through
        the natural numbers.
  .*    Square it.
  \     Switch to Ordinal mode.
  !     Store the square (as a string) on the tape.
  d     Push the concatenation of the entire stack (i.e. of all squares before
        the current one).
  ?~    Retrieve a copy of the current square and put it underneath.
  ?     Retrieve another copy.
  F     Find. If the current square is a substring of the previous squares,
        this results in the current square. Otherwise, this gives an empty
        string.
  $     If the previous string was empty (not an early bird) skip the next
        command.
  /     Switch back to Cardinal. This is NOT a command.
  W     Discard one address from the return address stack, decrementing our
        main loop counter if we've encountered an early bird.
K     Jump back to the beginning of the loop if any copies of the return
      address are left. Otherwise do nothing and exit the loop.

dt    Push the stack depth and decrement it, to get the final result.

この言語はわかりませんが、追加する前に現在の正方形が文字列に含まれているかどうかを確認して、バイトを節約できますか?
WGroleau

@WGroleauそうは思いません。メインチェックは(1バイトではFなくz)1バイトのままですが、スタック操作はこれ以上簡単ではなく、1つまたは2つのコマンドでさえ悪化する可能性があります。
マーティンエンダー

@JamesHoldernessなぜそうならないのですか?69696は、その自然な位置の2つ前の位置に表示されます(重なる)。その自然な位置との重複を無視する必要がある場合は、おそらく挑戦でそう言うべきです。
マーティン・エンダー

@JamesHoldernessでは、関連するテストケースを確認するのに時間がかかりすぎていたので、最大10個まで実行しました。中間テストケースが役立つはずです。
マーティン・エンダー

それは間違いなく挑戦を増やします。同じ方法で失敗する以前の回答をコメントしますか?注:これらの娯楽は面白いと思いますが、答えはありません。私の言語はすべて、読みやすさを要件として設計されているためです。:-)アセンブラとFORTHを除きます。:-)
WGroleau

1

、13バイト

!f§€oṁ₁ŀ₁N
d□

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

説明

2行目は、数値の2乗の10進数を提供するヘルパー関数です。

 □    Square.
d     Base-10 digits.

を使用して、メインプログラムでこの関数を呼び出すことができます。

!f§€oṁ₁ŀ₁N
 f§      N    Filter the list of natural numbers by the following fork g(n).
       ŀ        Get [0, 1, ... n-1]
     ṁ₁         Get the decimal digits of each value's square and concatenate
                them into one list. (A)
        ₁       And get the decimal digits of n² itself. (B)
    €           Check whether (A) contains (B) as a sublist.
!             Use the programs input as an index into this filtered list.


1

Wolfram言語(Mathematica)、75バイト

(n=k=0;s="";While[n<#,If[!StringFreeQ[s,t=ToString[++k^2]],n++];s=s<>t];k)&

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

使い方

nこれまでに見つかった初期鳥の数k、最後にチェックされた数s、文字列を保持します"1491625..."。一方でnは小さすぎる場合、s次の正方形が含まれている私たちはインクリメントので、別の初期の鳥は、発見されましたn。いずれにしても、拡張しsます。

一度nに達する入力#、我々は返すk、最後の番号がチェックされ、したがって、最後の早起きが見つかりました。

私のラップトップでは、シーケンスの53番目の項を計算するのに約53秒かかります。



1

バッシュ、 76 69バイト

nが変数に与えられていると仮定します(つまりn=10 foo.sh)。パッケージを使用しますgrep。中間の値が出力されます(許可されている場合、-3バイト)。

while((n));do((b=++a*a));grep -q $b<<<$s&&((n--));s=$s$b;done;echo $a

どのように機能しますか?

while ((n)); do  # while n != 0 (C-style arithmetic)
  ((b = ++a*a))  # Increment a and let b = a*a
    # Non-existent value is treated as zero
  grep $b<<<$s   # Search for b in s
    && ((n--))   # If found, decrement n
  s=$s$b         # Append b to s
done
echo $a

@ジェームズここに行きます。
iBug
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.