中間のオッズを伴う偶数桁の循環シーケンス


13

次のシーケンスを検討してください。

1, 0, 1, 2, 4, 1, 6, 8, 0, 1, 2, 4, 6, 8, 1, 0, 2, 4, 6, 8, 1, 0, 2, 4, 6, 8, 0, 1, ...

偶数桁は0から始まり、長さが増加するランにグループ化されます。これらは循環的に配置されます。つまり、8に達するまで昇順でソートされ、その後0から循環します1は偶数桁の実行を分離し、シーケンスも開始します。このシーケンスがどのように形成されるかを視覚化しましょう:

                 1, 0, 1, 2, 4, 1, 6, 8, 0, 1, 2, 4, 6, 8, 1, 0, 2, 4, 6, 8, 1,  ...

                    -     ----     -------     ----------     -------------
run length:         1      2          3            4                5            ...
position of 1:   X     X        X           X              X                 X   ...
even sequence:      0,    2, 4,    6, 8, 0,    2, 4, 6, 8,    0, 2, 4, 6, 8      ...

許容される入出力メソッド:

  • 入力として整数Nを受け取り、このシーケンスのN番目の項を出力します。

  • 入力として整数Nを受け取り、このシーケンスの最初のN項を出力します。

  • シーケンスを無期限に印刷します。

最初の2つの方法では、0または1のインデックス付けを選択できます。

標準の入出力メソッドを使用しながら、任意のプログラミング言語で競争できます標準的な抜け穴は禁止されています。これはであるため、各言語で最も短いコードが優先されます。


回答:



7

ゼリー、10バイト

5ḶḤṁR€1pFḣ

シーケンスの最初のnアイテムを返します。

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

使い方

5ḶḤṁR€1pFḣ  Main libk. Argument: n

5           Set the return value to 5.
 Ḷ          Unlength; yield [0, 1, 2, 3, 4].
  Ḥ         Unhalve; yield [0, 2, 4, 6, 8].
    R€      Range each; yield [[1], [1, 2], [1, 2, 3], ..., [1, ..., n]].
   ṁ        Mold; in the result to the left, replace [1] with [0], [1, 2] with
            [2, 4], [1, 2, 3] with [6, 8, 0], and so forth.
      1p    Take the Cartesian product of [1] and the result.
        F   Flatten the result.
         ḣ  Head; take the first n items of the result.

2
ಠ_ಠ Unhalve...それだけではありませんDoubleか?
ミスターXcoder

4
それは単なるニーモニックです。H半分であり半分ないÆAアークコサインであるためÆẠ非アルコサインも同様です。
デニス

6

12 11 10バイト

ṁ:1CN¢mDŀ5

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

シーケンスを無期限に印刷します。

代わりに:

J1CΘN¢mDŀ5

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

説明

        ŀ5   Start from [0, 1, 2, 3, 4]
      mD     Double each value to get [0, 2, 4, 6, 8]
     ¢       Repeat this list indefinitely, [0, 2, 4, 6, 8, 0, 2, ...]
   CN        Cut it into chunks of increasing lengths, 
             [[0], [2, 4], [6, 8, 0], ...]
ṁ:1          Prepend 1 to each sublist and concate the resulting lists.

代替ソリューションの場合:

     ¢mDŀ5   Again, get [0, 2, 4, 6, 8, 0, 2, ...].
  CΘN        This time we prepend a zero to the natural numbers, which
             prepends an empty list to the resulting chunks.
J1           Join all the sublists with 1.

また、整数のリストにはゼロを追加し、リストのリストには空のリストを追加する「既定の要素を追加する」...ΘCN...ため、実行することもできΘます。




2

APL、25バイト

n番目の項を返します。

1,(⌽n↑⌽(+/⍳n←⎕)⍴0,2×⍳4),1

説明

n←⎕     Prompts for screen input of integer
+/⍳      Creates a vector of integers of 1 to n and sums
⍴0,2×⍳4  Creates a vector by replicating 0 2 4 6 8 to the length of sum
⌽n↑⌽   Rotates the vector, selects first n elements and rotates result
        (selects last n elements}    
1,...,1 Concatenates 1s in front and behind result

2

APL(Dyalog Unicode)52 59 56バイト

rc k
r←~n0
:For j :In k
n+←j-1
r,←1,⍨jn0,2×⍳4
:End
rkr

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

これはtradfn(あるトラッド itional F unctio N)一つの引数を取るkと、第1復帰kシーケンスの項目を。

関数のエラーを指摘してくれた@GalenIvanovに感謝します。@Adámに3バイトをありがとう。

使い方:

rc k              The function c takes the argument k and results in r
r n1 0            Initializing the variables r and n, and setting them to 1 and 0, respectively.
:For j :In k      For loop. k yields [1, 2, 3, ..., k], and j is the control variable.
n+←j-1             Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...)
r,←1,⍨jn0,2×⍳4   This line is explained below.
:End               Ends the loop
rkr              return the first k items of r.
                    actually reshapes the vector r to the shape of k;
                   since k is a scalar,  reshapes r to a vector with k items.
            2×⍳4   APL is 1-indexed by default, so this yields the vector 2 4 6 8
          0,       Prepend a 0 to it. We now have 0 2 4 6 8
        n         Rotate the vector n times to the left.
      j           Reshape it to have j items, which cycles the vector.
   1,⍨             Append a 1, then
r,←                Append everything to r.

以下は、Dfnd irect f unctio n)とチャレンジを解決する暗黙の関数で、両方とも@Adámから提供されています。


暗黙のバージョンの説明に興味があります。ありがとう!
ガレンイワノフ

@GalenIvanov今日は後で追加します。
J.サレ

3つの関数の答えが正しくないことに気付きました-サブシーケンスは常に1の後の0から始まります-それらは前のサブシーケンスの最後の偶数桁から続くはずです。
ガレンイワノフ

@GalenIvanovそのとおりです。今日修正して説明を追加できるかどうかを確認します。
J.サレ

1

JavaScript(ES6)、62 54 52バイト

シーケンスのN番目の項を0インデックスで返します。

n=>(g=e=>n--?g(e+=k++<l?2:k=!++l):k<l?e%10:1)(k=l=0)

デモ


1

C(gcc)、84バイト

i;j;f(){for(i=1;;i++){printf("%d ",1);for(j=0;j<i;)printf("%d ",(2*j+++i*i-i)%10);}}

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

f()シーケンスをスペースで区切って無限に印刷する関数()。

i 現在の偶数実行の長さです。

j 現在の偶数実行のインデックスです

(2*j+++i*i-i)%10 (j + Tr(i))%5)* 2と等価なiおよびj(およびjをインクリメント)が与えられると、正しい偶数を返します。ここで、Tr(x)はx番目の三角数(偶数の数)現在の偶数実行前に印刷された数字。



1

Java 8、96バイト

v->{for(int i=0,j=-2,k;;i++,System.out.println(1))for(k=0;k++<i;System.out.println(j%=10))j+=2;}

各番号を新しい行に無期限に印刷します。

説明:

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

v->{                               // Method with empty unused parameter and no return-type
  for(int i=0,                     //  Index integer, starting at 1
          j=-2,                    //  Even index integer, starting at -2
          k;                       //  Inner index integer
      ;                            //  Loop (1) indefinitely
                                   //    After every iteration:
       i++,                        //     Increase index `i` by 1
       System.out.println(1))      //     And print a 1
    for(k=0;                       //   Reset index `k` to 0
        k++<i;                     //   Inner loop (2) from 0 to `i`
                                   //     After every iteration:
       System.out.println(j%=10))  //      Set `j` to `j` modulo-10, and print it
      j+=2;                        //    Increase `j` by 2
                                   //   End of inner loop (2) (implicit / single-line body)
                                   //  End of loop (1) (implicit / single-line body)
}                                  // End of method

1

バッチ、85バイト

@set/an=%2+1,t=n*-~n/2
@if %t% lss %1 %0 %1 %n%
@cmd/cset/a(%1-n)%%5*2*!!(t-=%1)+!t

シーケンスのN番目の項を出力します。次の三角数を計算することにより機能します。



1

J46 42 40バイト

コールのおかげで-6バイト

{.({.[:;[:#:&.>2^i.);@(1,&.><;.1)10|2*i.

このシーケンスの最初のN項を出力します。

使い方:

10|[:+:i. -0 2 4 6 8 0 2 4 ...の長さNのリストを生成します... 0から始まる整数のリストの2倍の項目のmod 10を単純に取ります。

[:;[:#:&.>2^i. -上記のリストを切り取るためのビットマスクを生成します。

(1は開始を意味します):1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 ... 2の連続する非負整数のべき乗を求め、それらをバイナリに変換し、平坦化します両方のリストの長さが同じになるように、最初のN項目のみをリストします。

;@(1,&.><;.1) -1と0のマップに従って偶数桁のリストをサブリストに分割(カット)し、サブリストを1に追加して、最終的に結果のリストをフラット化します

]{. -最初のN個のアイテムのみを受け取り、1が追加されたためにリスト内の追加の番号を取り除きます。

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


1
42バイト:{.({.[:;[:#:&.>2^i.);@(1,&.><;.1)(10|2*i.)。私が行った変更は、フックを使用し、フォークの正しいタインをリファクタリングして、フォークの仕組みを活用することでした。2^i.トリックが好きです。私は今、フォークの左脚に取り組んでいます。
コール

@coleおかげで、フォークをより良く使うことを学ぶ必要があります。どうやらフォーク2 * i。キャップ付きフック[:+:i。よりも優れています。
ガレンイワノフ

1
右の歯に括弧をドロップすることもできます(10|2*i.)->10|2*i.
コール








0

JavaScript、45バイト

1インデックス作成済み:

f=(x,t=0,p=0)=>p<x?f(x-1,t+1,p+t):x-p?x%5*2:1

0インデックス:

g=(x,p=0)=>p<x?g(x-1,p?++t+p:t=1):x-p?x%5*2:1



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