เหตุการณ์แรกในลำดับ Sixers


17

ลำดับซิกส์เป็นชื่อที่จะได้รับการลำดับA087409 ฉันเรียนรู้เกี่ยวกับลำดับนี้ในวิดีโอ Numberphileและสามารถสร้างได้ดังนี้

ก่อนอื่นคูณด้วย 6 เขียนในฐาน 10:

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

ถัดไปต่อตัวเลขให้เป็นตัวเลข:

61218243036...

ในที่สุดจัดกลุ่มสตรีมเป็นคู่และตีความแต่ละรายการเป็นจำนวนเต็ม:

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

เมื่อเราจัดกลุ่มตัวเลขเป็นคู่จำนวนสูงสุดในลำดับจะเท่ากับ 99 และปรากฎว่าจำนวนเต็มไม่ใช่ลบทั้งหมดน้อยกว่า 100 แสดงในลำดับ ความท้าทายนี้คือการหาดัชนีของอินสแตนซ์แรกของตัวเลขในลำดับ Sixers

อินพุต

[0-99]จำนวนเต็มในช่วง คุณไม่จำเป็นต้องบัญชีตัวเลขนอกช่วงนี้และวิธีการแก้ปัญหาของคุณอาจมีพฤติกรรมใด ๆ หากมีการป้อนข้อมูลดังกล่าว

เอาท์พุต

ดัชนีของการเกิดขึ้นครั้งแรกของหมายเลขอินพุตในลำดับ Sixers นี่อาจเป็น 0- หรือ 1-indexed; โปรดบอกสิ่งที่คุณกำลังใช้ในคำตอบของคุณ

กฎระเบียบ

  • ขั้นตอนในการสร้างลำดับที่บันทึกไว้ในบทนำมีไว้เพื่อเป็นตัวอย่างเท่านั้นคุณสามารถใช้วิธีการใดก็ได้ที่คุณต้องการตราบใดที่ผลลัพธ์เหมือนกัน
  • คุณสามารถส่งโปรแกรมหรือฟังก์ชั่นเต็มรูปแบบ
  • อนุญาตให้ใช้วิธีการอินพุตและเอาต์พุตใด ๆ ที่เหมาะสม
  • ช่องโหว่มาตรฐานไม่ได้รับอนุญาต
  • ลิงค์เพื่อทดสอบรหัสของคุณออนไลน์ขอแนะนำ!
  • นี่คือคำตอบที่สั้นที่สุดในแต่ละภาษาชนะ!

กรณีทดสอบ

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นั้นเพียงพอที่จะสร้างคุณค่าที่เป็นไปได้ทั้งหมด
หลุยส์เมนโด

@ LuisMendo คุณพูดถูกฉันกำลังถกเถียงกันว่าจะรวมไว้ในคำอธิบายการท้าทายหรือไม่ ความคิดเห็นก็เป็นสถานที่ที่ดีเช่นกัน: o)
Sok

เราสามารถใช้การป้อนข้อมูลที่เป็นจำนวนเต็มสตริงกับคนที่เบาะมี 0 นำ (เช่น, , , ... )? n<10000102
Kevin Cruijssen

10
@KevinCruijssen Hmmm การป้อนข้อมูลเป็นสตริงนั้นใช้ได้ แต่การเว้นว่างด้วย 0 เป็นบิตที่ไกลเกินไป IMO
Sok

คำตอบ:


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 ตัวอักษรแรกของสตริงของการเชื่อมต่อหลายรายการที่6หรือต่อท้ายอักขระใหม่ถ้าเรามีน้อยกว่า 2 ตัว

ตัวอย่างสำหรับ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

Python 2 , 93 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จะเหมือนกันสำหรับฟังก์ชั่นมากที่สุดเช่นการตรวจสอบที่เท่าเทียมกันและดังกล่าว ยังคงมีการส่งต่อไปยังฟังก์ชัน int และ cast to string ใน 05AB1E สำหรับฟังก์ชันบางอย่างเช่นการเรียงลำดับ (การเรียงลำดับตัวเลขและการเรียงลำดับพจนานุกรม) หรือสำหรับการลบตัวเลขทศนิยมหลังจากเครื่องหมายจุลภาคจากการลอยเมื่อชี้ไปยัง int แต่ไม่ได้ใช้บ่อย .
Kevin Cruijssen

@maxb เคล็ดลับ 05AB1E ที่เกี่ยวข้องให้ตัวอย่างเพิ่มเติม
Kevin Cruijssen

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 ใช่มันเป็นreshape ของ APL ดังนั้นถ้าคุณต้องการที่จะทำให้เวกเตอร์แบนคุณต้องทำ<new shape vector> ⍴ <vector to reshape>
J. Sallé

คุณสามารถใช้ reshape เพื่อสร้างรายการ 2xN แล้วแปลงเป็นจำนวนเต็มได้หรือไม่
streetster

คุณทำได้ แต่ฉันไม่คิดว่าจะสั้นกว่าคำตอบปัจจุบันของฉัน ปัญหาหนึ่งคือสำหรับคำตอบของฉันการเปลี่ยนสตริงเป็นเมทริกซ์ 1117 × 2 จากนั้นการแปลงเป็นจำนวนเต็มจะสร้างเวกเตอร์ด้วย 1117 ซึ่งเป็นจำนวนเต็มหนึ่งหลัก ดูความแตกต่างระหว่างวิธีการที่ฉันใช้กับการปรับรูปร่าง
J. Sallé

อาของฉันบี้สตริงได้รับการเปลี่ยนโฉมหน้าเป็นสิ่งที่สามารถทำงานได้มาก :)
streetster


3

ภาษา Wolfram (Mathematica) , 74 ไบต์

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

ลองออนไลน์!

บันทึก 2 ไบต์จาก @Expired Data



ฉันทำงานเกี่ยวกับเรื่องนี้ก่อนที่จะเห็นคุณ .. รวมพวกเขาและได้77 ไบต์
ข้อมูลหมดอายุ

@ ExpiredData ดี 74 ไบต์ในขณะนี้
J42161217



2

MathGolf , 10 ไบต์

•╒6*y░2/i=

ลองออนไลน์!

โดยทั่วไปเหมือนกับคำตอบ 05AB1E แต่ฉันทำไบต์ที่หายไปโดยต้องแปลงจำนวนที่ตัดแบ่งเป็นสตริงอย่างชัดเจน

คำอธิบาย

•╒             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 คอมไพเลอร์) , 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 's งูหลาม 2 คำตอบ
-2 ไบต์ขอบคุณที่@Imus

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
คุณสามารถบันทึก 2 ไบต์โดยใช้ "" + n / 10 + n% 10 แทน n> 9? n + "": "0" + n
Imus

1

CJam , 17 ไบต์

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

ลองออนไลน์!

0-based

คำอธิบาย

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

ด้วยความอยากรู้อยากเห็นเหตุใด CJam จึงมีโครงสร้างสำหรับจำนวนเต็มทั้งหมดในช่วง [10,20]รวมทั้งบิลด์ต่าง ๆ ห้าบิลด์ที่เริ่มต้นเป็นสตริงว่างเปล่า""แต่ไม่มีบิลด์สำหรับ100 หรือ 1000?
Kevin Cruijssen

@KevinCruijssen ไม่แน่ใจ ... แต่การมีตัวแปรที่มีค่าที่กำหนดไว้ล่วงหน้าเช่น0หรือ""บางครั้งมีประโยชน์สำหรับลูปเพราะมักเป็นค่าเริ่มต้นที่ต้องการ สำหรับการไม่มี100หรือ1000ใช่ฉันยอมรับว่าพวกเขาจะมีประโยชน์มากกว่าที่จะพูด18หรือ19
หลุยส์เมนโด

1
มันเป็นความอัปยศที่เลขศูนย์นำหน้าเป็นที่น่ารำคาญมิฉะนั้นคุณสามารถทิ้ง:~และiจากโค้ดของคุณ :(
Erik the Outgolfer

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

เรติน่า 0.8.2 , 36 ไบต์

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

ลองออนไลน์! การเชื่อมโยงรวมถึงชุดทดสอบ 1 การจัดทำดัชนี คำอธิบาย:

^
2406$*_

คำนำหน้า 2406 _s กับอินพุต

_{6}
$.`

เปลี่ยนทุกๆ 6 _วินาทีที่มีจำนวนของก่อน_s สิ่งนี้สร้างลำดับ0 , 6, 12... 2400แต่โดยอัตโนมัติเชื่อมตัวเลข

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

ข้าม 0 นำหน้าและค้นหาตัวเลขคู่แรกที่ตรงกับตัวเลขสองหลักสุดท้ายเช่นอินพุตเป็นศูนย์ (เนื่องจากสตริงลงท้ายด้วย 0ในความจริงแล้วชุดทดสอบใช้ความจริงที่ว่าลงท้ายด้วย00)

$#1

เอาท์พุทจำนวนคู่ของตัวเลขถึงและรวมถึงการแข่งขัน

Retina 1 ช่วยประหยัดสองสามไบต์เพราะผู้ประกอบการทำซ้ำสตริงของมันคือไบต์ที่สั้นกว่าและเป็นค่าเริ่มต้นและคูณด้วย 6 ในการทดแทน สิ่งนี้ไม่ส่งผลกระทบต่อจำนวนไบต์โดยรวมเนื่องจากผลลัพธ์สุดท้ายมีลักษณะดังนี้:_2406*เป็นตัวถูกดำเนินการทางด้านขวามือของตนเพื่อที่บรรทัดที่สองของรหัสจะกลายเป็นเพียง คุณลักษณะอีกประการของ Retina 1 คือ>ตัวดัดแปลงที่สร้างการทดแทนในบริบทของตัวแยกหลังการแข่งขันซึ่งในกรณีที่$.>`มันทำให้มันรวมความยาวของการแข่งขันในผลลัพธ์ แม้ว่าจะมีค่าใช้จ่ายไบต์เราบันทึกทันทีที่เราไม่จำเป็นต้องตรงกับ0อีกต่อไป (ซ้ำจะต้องลดลง 6) จอประสาทตา 1 ยังสามารถทำเลขคณิตพื้นฐานในการทดแทน ซึ่งหมายความว่าเราไม่จำเป็นต้องใช้กลอุบายในการคูณจำนวน 6 แทนเราแค่สร้างตัวเลข1..400

^
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;}

ลองออนไลน์!

พอร์ตอื่นของJavaและPython เป็นคำตอบ

คำตอบเดิมของฉันด้านล่าง:

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 ฐาน



โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.