Sixersシーケンスの最初の出現


17

Sixersシーケンスは、シーケンスに与えることができる名前ですA087409Numberphileビデオでこのシーケンスについて学びました。次のように構成できます。

まず、基数10で書かれた6の倍数を取ります。

6, 12, 18, 24, 30, 36, ...

次に、数字を数字のストリームに連結します。

61218243036...

最後に、ストリームをペアに再グループ化し、それぞれを整数として解釈します。

61, 21, 82, 43, 3, ...

数をペアにグループ化するため、シーケンスの最大数は99になり、100未満のすべての非負整数がシーケンスで表されることがわかります。この課題は、Sixersシーケンス内の数値の最初のインスタンスのインデックスを見つけることです。

入力

範囲内の整数[0-99]。この範囲外の数値を考慮する必要はありません。そのような入力が与えられた場合、ソリューションは任意の動作をすることができます。

出力

Sixersシーケンスで入力番号が最初に現れるインデックス。これは、0インデックスまたは1インデックスの場合があります。答えの中でどれを使っているかを言ってください。

ルール

  • 導入部に記載されているシーケンスを生成する手順は、例示のみを目的としており、結果が同じである限り、任意の方法を使用できます。
  • 完全なプログラムまたは機能を送信できます。
  • 入力および出力の賢明な方法はすべて許可されます。
  • 標準の抜け穴は許可されていません。
  • オンラインでコードをテストするためのリンクをお勧めします!
  • これはなので、各言語の最短回答が勝ちです!

テストケース

以下に、すべての入力と出力のリストをフォーマットで示しますinput, 0-indexed output, 1-indexed output

0   241 242
1   21  22
2   16  17
3   4   5
4   96  97
5   126 127
6   9   10
7   171 172
8   201 202
9   14  15
10  17  18
11  277 278
12  20  21
13  23  24
14  19  20
15  29  30
16  32  33
17  297 298
18  35  36
19  38  39
20  41  42
21  1   2
22  46  47
23  69  70
24  6   7
25  53  54
26  22  23
27  11  12
28  62  63
29  219 220
30  65  66
31  68  69
32  71  72
33  74  75
34  49  50
35  357 358
36  80  81
37  83  84
38  25  26
39  89  90
40  92  93
41  27  28
42  42  43
43  3   4
44  101 102
45  104 105
46  8   9
47  177 178
48  110 111
49  13  14
50  28  29
51  119 120
52  122 123
53  417 418
54  79  80
55  128 129
56  131 132
57  134 135
58  55  56
59  437 438
60  140 141
61  0   1
62  31  32
63  75  76
64  5   6
65  120 121
66  82  83
67  10  11
68  161 162
69  164 165
70  58  59
71  477 478
72  170 171
73  173 174
74  34  35
75  179 180
76  182 183
77  497 498
78  85  86
79  188 189
80  191 192
81  18  19
82  2   3
83  78  79
84  93  94
85  7   8
86  37  38
87  168 169
88  12  13
89  228 229
90  88  89
91  218 219
92  221 222
93  224 225
94  64  65
95  557 558
96  230 231
97  233 234
98  40  41
99  239 240

6
6, 2*6, 3*6,..., 325*6すべての可能な値を生成するために考慮することで十分であることを知っていると便利かもしれません
Luis Mendo

@LuisMendoその通り、チャレンジの説明にそれを含めるかどうかを議論していました。コメントも良い場所です:o)
ソク

私たちはもので、文字列として入力整数を取ることができ有数0でパディング(すなわち、、、...)?n<10000102
ケビンクルーッセン

10
@KevinCruijssenうーん、文字列としての入力は問題ありませんが、0での左パディングは少し遠すぎますIMO。
ソク

回答:


12

JavaScript(ES6)、 71 65  55バイト

出力は0から始まります。

n=>(g=([a,b,...c])=>b?a+b-n&&1+g(c):g([a]+6*++i))(i='')

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

どうやって?

再帰関数を使用して、倍数の連結文字列の最初の2文字を「消費」するか、2文字未満の場合は新しい文字を追加します。6

例:n=3

 string | operation                          | result
--------+------------------------------------+--------
 ''     | not enough characters: append '6'  |   0
 '6'    | not enough characters: append '12' |   0
 '612'  | consume '61', increment the result |   1
 '2'    | not enough characters: append '18' |   1
 '218'  | consume '21', increment the result |   2
 '8'    | not enough characters: append '24' |   2
 '824'  | consume '82', increment the result |   3
 '4'    | not enough characters: append '30' |   3
 '430'  | consume '43', increment the result |   4
 '0'    | not enough characters: append '36' |   4
 '036'  | consume '03': success              |   4

コメント済み

n => (             // n = input
  g = (            // g is a recursive function taking either a string or an array of
                   // characters split into:
    [a, b,         //   a = 1st character, b = 2nd character,
           ...c]   //   c[] = array of all remaining characters
  ) =>             //
    b ?            // if b is defined:
      a + b - n && //   if n subtracted from the concatenation of a and b is not zero:
        1 + g(c)   //     add 1 to the final result and do a recursive call with c[]
                   //   (otherwise: yield 0 and stop recursion)
    :              // else:
      g(           //   do a recursive call with:
        [a] +      //     the concatenation of a (forced to an empty string if undefined)
        6 * ++i    //     and 6 * i, with i pre-incremented
      )            //   end of recursive call
)(i = '')          // initial call to g with an empty string,
                   // and i set to empty string as well (zero'ish)

12

パイソン293 92 85 83 81 68の 65の 59バイト

f=lambda n,s='612',i=18:n-int(s[:2])and-~f(n,s[2:]+`i`,i+6)

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


  • -2バイト、Grimyのおかげ
  • -3バイト、ArBoのおかげ
  • -6バイト、xnorのおかげ

1
ラムダとして3バイト短縮:f=lambda n,s='612',i=3:n-int(s[:2])and f(n,s[2:]+`i*6`,i+1)or i-2
ArBo

@ArBoさらに良い、f=lambda n,s='612',i=18:n-int(s[:2])and-~f(n,s[2:]+`i`,i+6)(0インデックス付き)。
xnor

8

Perl 6、31バイト

{+(comb(2,[~] 1..ⅮX*6)...$_)}

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

1インデックスのシーケンスを使用します。

説明:

{                            } # Anonymous code block
              1..Ⅾ             # The range 1 to 500
                   X*6         # All multiplied by 6
          [~]                  # Join as one giant string
   comb(2,            )        # Split into pairs of characters
                       ...$_   # Take up to the input
 +(                         )  # And return the length of the list


5

05AB1E、9 バイト

₄L6*J2ôIk

0インデックス付き。単一の整数または整数のリストを入力として受け入れます。

オンラインそれを試してみたり、すべてのテストケースを確認してください

説明:

L         # Create a list in the range [1,1000]
  6*       # Multiply each value by 6
    J      # Join the entire list of integers together to a string
     2ô    # Split into parts of size 2
       Ik  # Get the index of the input integer(s)
           # (and output the result implicitly)

文字列として結合するデフォルトの動作ですか、または文字列として結合して数値として結合するための別個の演算子がありますか?
maxb

@maxb 05AB1E全体では、明示的な変換は必要ありません。すべての整数は、置換や分割などの文字列関数にも使用でき、作成されたすべての文字列(整数)も数値として使用できます。そう100"100"100.0同等のチェックと、そのようななどのほとんどの機能のために同じです。05AB1Eには、並べ替え(数値と辞書式の並べ替え)、またはintにキャストするときにfloatからコンマの後の10進数字を削除するなどの機能のために、intにキャストし、文字列にキャストする関数がまだありますが、それほど頻繁には使用されません。
ケビンクルーイッセン

@maxb 関連する05AB1Eのヒントにいくつかの追加例を示します。
ケビンクルーッセン

4

、12バイト

I⌕I⪪⭆φ×⁶⊕ι²N

オンラインでお試しください!リンクは、コードの詳細バージョンです。0インデックス。説明:

     φ           Predefined constant 1000
    ⭆           Map over implicit range and join
        ι       Current index
       ⊕        Incremented
     ×⁶         Multiplied by 6
   ⪪      ²     Split into pairs of digits
  I             Cast to integer
           N    Input as a number
 ⌕              Find its index
I               Cast to string
                Implicitly print


4

APL(Dyalog Unicode)、26バイト

{⍵⍳⍨⍎¨((≠\=⍨)⊂⊢)∊⍕¨6×⍳325}

オンラインでお試しください!-すべての有効な入力のテスト。

どうやって:

{⍵⍳⍨⍎¨((≠\=⍨)⊂⊢)∊⍕¨6×⍳325}  Dfn, input is ⍵.
                    6×⍳325   Generates the first 325 multiples of 6.
                  ⍕¨         Format each number into a string
                            Enlist, flattens the vector
       (      ⊂⊢)            Dyadic enclose, takes a boolean mask as left argument
        (≠\=⍨)               Generates the mask 1 0 1 0...
                             Enclose then returns the Sixers sequence as a string
     ⍎¨                      Execute each element in the string, turning it into a numeric vector
 ⍵⍳⍨                         Find the first occurrence of  in the vector

Kでできるように、平坦化されたベクトルを変形できますか?Googleが提案するが、APLは...私をおびえさせる
streetsterを

@streetsterはい、APLの改造です。したがって、平坦化されたベクトルの形状を変更する場合は、<new shape vector> ⍴ <vector to reshape>
J

あなたはreshapeを使用して2xNリストを作成し、それぞれを整数に変換できますか?
ストリートスター

可能ですが、それが現在の答えよりも短いとは思いません。1つの問題は、私の答えでは、文字列を1117×2行列に再形成し、整数に変換すると1117、1桁の整数を持つベクトルが作成されることです。違いを参照してください。私が使用している方法と比べ整形
J.サール

ああ、私の平らな文字列は、より実行可能なものに再形成されます:)
ストリートスター


3

Wolfram言語(Mathematica)、74バイト

#&@@Position[FromDigits/@Flatten@IntegerDigits[6Range@365]~Partition~2,#]&

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

@Expired Dataから2バイト保存



私はあなたのものを見る前にこれに取り組んでいた..それらをマージし、77バイト
期限切れのデータ

@ExpiredDataいいね。現在74バイト
J42161217



2

MathGolf、10バイト

•╒6*y░2/i=

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

基本的に05AB1Eの回答と同じですが、連結された数値を明示的に文字列に変換する必要があるため、1バイトを失います。

説明

•╒             push [1, 2, ..., 512]
  6*           multiply by 6
    y          join array without separator to string or number
     ░         convert to string (implicit map)
      2/       split into groups of 2 characters
        i      convert to integer (implicit map)
         =     find index of implicit input in the array


2

C#(Visual C#Interactive Compiler)123バイト 115バイト

a=>m.First(y=>int.Parse(string.Join("",m.Select((x,i)=>++i*6)).Substring(y*2,2))==a);var m=Enumerable.Range(0,640);

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


ソリューションにバグがあると思います。f(61)返されるはずです0(ソリューションのインデックスは0になっているようです)
ソク

1
ありがとう@sokは今修正されるべきです
有効期限切れデータ

2

K(oK)、22バイト

解決:

(.:'0N 2#,/$6*1+!999)?

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

説明:

0インデックス付き。

(.:'0N 2#,/$6*1+!999)? / the solution
                     ? / lookup right in left
(                   )  / do this together
                !999   / range 0..999
              1+       / add 1, range 1...1000
            6*         / multiply by 6, 6...6000
           $           / convert to strings
         ,/            / flatten
    0N 2#              / reshape into 2xN
 .:'                   / value each, convert to numbers


2

Java 10、119 104 102バイト

n->{int i=2;for(var s="612";!s.substring(0,2).equals(""+n/10+n%10);)s=s.substring(2)+6*++i;return~-i;}

@TFeldのPythonの2の答え@Imusの
おかげで-2バイト。

1インデックス付き。

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

説明:

n->{                            // Method with integer as both parameter and return-type
  int i=2;                      //  Index-integer, starting at 2
  for(var s="612";              //  String, starting at "612"
      !s.substring(0,2)         //  Loop as long as the first two characters of the String
       .equals(                 //  Are not equal to:
               ""+n/10          //   The input integer-divided by 10 as String
               +n%10);)         //   Concatenated with the input modulo-10
                                //   (which will add leading 0s for inputs < 10)
    s=s.substring(2)            //   Remove the first two characters of the String
      +6*++i;                   //   And append 6 times `i`,
                                //   after we've first increased `i` by 1 with `++i`
return~-i;}                     //  Return `i-1` as result

元の119 117バイトバージョン:

n->{var s="";for(int i=0;i<2e3;)s+=i+=6;return java.util.Arrays.asList(s.split("(?<=\\G..)")).indexOf(""+n/10+n%10);}

0インデックス付き。

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

説明:

n->{                            // Method with integer as both parameter and return-type
  var s="";                     //  String we're building, starting empty
  for(int i=0;i<2e3;)           //  Loop `i` in the range [0, 2000):
      s+=i+=6;                  //   Increase `i` by 6 first every iteration
                                //   And then append the updated `i` to String `s`
  return java.util.Arrays.asList(
          s.split("(?<=\\G..)") //  Split the String in parts of size 2 (as array)
         )                      //  Convert the array to a List
          .indexOf(             //  And get the index of the following in this list:
                   ""+n/10      //   The input integer-divided by 10 as String
                   +n%10);}     //   Concatenated with the input modulo-10

1
あなたは" "?10の代わりに、N> 9のn + / 10 + N%のn +""を使用して2つのバイトを保存することができます: "0" + N
アイマス

1

CJam、17バイト

325,:)6f*s2/:~ri#

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

0ベース。

説明

325,   e# Range [0 1 2 ... 324]
:)     e# Add 1 to each: gives [1 2 3 ... 325]
6f*    e# Multiply each by 6: gives [6 12 18 ... 1950]
s      e# Convert to string: gives "61218...1950"
2/     e# Split into chunks of size 2: gives ["61" "21" ... "95" "0"]
       e# Note how the last chunk has size 1; but it is not used
:~     e# Evaluate each string in that array: gives [61 21 ... 95 0]
ri     e# Read input as an integer
#      e# Index of fist occurrence, 0-based

[10,20]""1001000

@KevinCruijssenわからない...しかし、0またはのような事前に定義された値を持つ変数を持つ""ことは、ループの場合に役立つことがあります。持っていないためとして100、または1000、はい、私は彼らが言うよりも有用だろう同意する1819
ルイス・Mendo

1
それはそうでなければ、捨てることができ、先行ゼロは迷惑している恥だ:~i、あなたのコードから。:(
エリック・ザ・アウトゴルファー

1

Japt、12バイト

0インデックス付き。

L²õ*6 ¬ò b¥U

試すかすべての入力をテストしてください

L²õ*6 ¬ò b¥U     :Implicit input of integer U
L                :100
 ²               :Squared
  õ              :Range [1,L²]
   *6            :Multiply each by 6
      ¬          :Join to a string
       ò         :Split to array of strings each of length 2
         b       :First 0-based index of
          ¥U     :Test for equality with U (bU wouldn't work here as each string would first need to be cast to an integer, costing more bytes)




1

網膜83 77バイト

私はRetinaでの複雑なプログラミングを実際に練習していませんが、何とかしてやってきたことに満足しています。

0インデックスの結果を出力します。

.+
6*1
325+-1%`1+
$0¶6*1$0
1+
$.0
¶

L`..
m`^0

$
¶$+
s`\b(\d+)\b.*\b\1$

C`¶

オンラインで試す


説明

.+                   Replace the input with 6 in unary
6*1
325+-1%`1+           Do 325 times: append line with previous + 6
$0¶6*1$0
1+                   Convert all lines to decimal
$.0
¶                    Remove line breaks

L`..                 List pairs of digits
m`^0                 Remove leading zeros

$                    Append the original input N on a new line
¶$+
s`\b(\d+)\b.*\b\1$   Remove occurrences of N and anything in between

C`¶                  Count the number of line breaks


1

Retina 0.8.2、36バイト

^
2406$*_
_{6}
$.`
^0(..)+?.*\1$
$#1

オンラインでお試しください!リンクにはテストスイートが含まれます。1インデックス付き。説明:

^
2406$*_

_入力の接頭辞2406 s。

_{6}
$.`

6 _秒ごとに先行する_の数で置き換えます。これは、シーケンスを生成し0612... 2400、しかし、自動的に番号を連結します。

^0(..)+?.*\1$

先頭の0をスキップして、最後の2桁に一致する最初の桁のペア、つまりゼロが埋め込まれた入力を見つけます(文字列が0;で終わるため、実際にテストスイートはで終わるという事実を使用します00)。

$#1

一致までの数字のペアの数を出力します。

Retina 1は、文字列の繰り返し演算子が1バイト短く、既にデフォルトで_右側のオペランドに設定されているため、コードの2行目がちょうど2 バイトになるため、数バイトを節約します2406*。Retina 1のもう1つの機能は>、マッチ後にセパレータのコンテキストで置換を生成する修飾子です。これ$.>`により、マッチの長さが結果に含まれます。これには1バイトかかりますが、0もう一致させる必要がないので、すぐに保存します。(繰り返しも6減らす必要があります。)Retina 1は、置換で基本的な算術演算を行うこともできます。つまり、6の倍数を取るためにトリックに頼る必要はなく、代わりに数字を生成するだけです。1..400代入で6を掛けます。驚くべきことに、これは最終的な結果が次のようになるため、全体的なバイト数にも影響しません。

^
400*
_
$.(6*$>`
^(..)+?.*\1$
$#1


1

C#(Visual C#Interactive Compiler)、88バイト

n=>{int i=2;for(var s="612";!s.StartsWith($"{n:d2}");s=s.Substring(2)+6*++i);return~-i;}

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

JavaPythonの別のポートが回答します。

以下の私の元の答え:

C#(Visual C#Interactive Compiler)、102バイト

n=>{dynamic s="",t=$"{n:d2}",i=0;for(;i++<400;s+=i*6);for(i=0;s[i++]!=t[0]|s[i++]!=t[1];);return i/2;}

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

どちらのソリューションも1ベースのインデックスを使用します。



1

Clojure、102バイト

#(count(for[i(partition 2(for[i(range 1 326)c(str(* i 6))]c)):while(not=(seq(str(if(< % 10)0)%))i)]i))

さよなら!:(

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