# First occurrence in the Sixers sequence

The Sixers sequence is a name that can be given to sequence A087409. I learned about this sequence in a Numberphile video, and it can be constructed as follows:

First, take the multiples of 6, written in base 10:

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

Next, concatenate the numbers into a stream of digits:

61218243036...

Finally, regroup the stream into pairs and interpret each as an integer:

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

As we're grouping the numbers into pairs, the maximum number in the sequence will be 99, and it turns out that all non-negative integers less than 100 are represented in the sequence. This challenge is to find the index of the first instance of a number in the Sixers sequence.

## Input

An integer in the range [0-99]. You do not need to account for numbers outside this range, and your solution can have any behaviour if such an input is given.

## Output

The index of the first occurrence of the input number in the Sixers sequence. This may be 0- or 1-indexed; please say which you are using in your answer.

## Rules

• The procedure to generate the sequence noted in the introduction is for illustrative purposes only, you can use any method you like as long as the results are the same.
• You can submit full programs or functions.
• Any sensible methods of input and output are allowed.
• Standard loopholes are disallowed.
• This is , so shortest answer in each language wins!

## Test cases

Here is a list of all input and outputs, in the format 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

• It may be useful to know that considering 6, 2*6, 3*6,..., 325*6 is sufficient to generate all possible values Commented Jun 14, 2019 at 7:37
• @LuisMendo You're right, I was debating whether to include that in the challenge description. A comment is a good place for it too :o)
– Sok
Commented Jun 14, 2019 at 7:44
• Can we take the input-integer as string, with the ones $n<10$ padded with a leading 0 (i.e. 00, 01, 02, ...)? Commented Jun 14, 2019 at 7:45
• @KevinCruijssen Hmmm, input as a string is fine, but left padding with 0 is a bit too far IMO.
– Sok
Commented Jun 14, 2019 at 7:51
• @LuisMendo I found (experimentally) that 295*6 is sufficient to generate needed values: 295*6 = 1770 of which the 77 is the digit pair with the highest index (497/498). Commented Sep 1, 2023 at 14:16

# JavaScript (ES6),  71 65  55 bytes

Output is 0-indexed.

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


Try it online!

### How?

Using a recursive function, we either 'consume' the first 2 characters of the string of concatenated multiples of $$\6\$$, or append new characters if we have less than 2 of them.

Example for $$\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


### Commented

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)


# Python 2, 93928583816865 59 bytes

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


Try it online!

• -2 bytes, thanks to Grimy
• -3 bytes, thanks to ArBo
• -6 bytes, thanks to xnor
• 3 bytes shorter as a lambda: f=lambda n,s='612',i=3:n-int(s[:2])and f(n,s[2:]+i*6,i+1)or i-2
– ArBo
Commented Jun 14, 2019 at 13:18
• @ArBo Even better, f=lambda n,s='612',i=18:n-int(s[:2])and-~f(n,s[2:]+i,i+6) (0-indexed).
– xnor
Commented Jun 15, 2019 at 1:07

# Perl 6, 31 bytes

{+(comb(2,[~] 1..ⅮX*6)...$_)}  Try it online! Uses the 1-indexed sequence. ### Explanation: { } # 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


# Haskell, 82 ... 65 58 54 bytes

k$show=<<[6,12..] k(a:b:c)t|read[a,b]==t=0|let=1+k c t  Try it online! # 05AB1E, 9 bytes ₄L6*J2ôIk  0-indexed. Accepts either a single integer, or a list of integers as input. Explanation: ₄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)  • Is the default behavior to join as string, or are there separate operators for joining as string and joining as number? – maxb Commented Jun 14, 2019 at 12:22 • @maxb Overall 05AB1E doesn't need any explicit conversions. All integers can also be used for string-functions like replace or split, and all created strings (which are integers) can also be used as numbers. So 100, "100", and 100.0 are the same for most functions like equal checks and such. There are still cast to int and cast to string functions in 05AB1E for some functionalities, like sorting (numeric vs lexicographic sorting), or for removing decimal digits after the comma from a float when casting to int, but they aren't used that often. Commented Jun 14, 2019 at 13:03 • @maxb Relevant 05AB1E tip giving some additional examples. Commented Jun 14, 2019 at 13:10 # APL (Dyalog Unicode), 26 bytes {⍵⍳⍨⍎¨((≠\=⍨)⊂⊢)∊⍕¨6×⍳325}  Try it online! - Tests for all valid inputs. ### How: {⍵⍳⍨⍎¨((≠\=⍨)⊂⊢)∊⍕¨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  • can you reshape the flattened vector like you can in K? Google suggests ⍴ but APL scares me... – mkst Commented Jun 15, 2019 at 20:25 • @streetster yes, ⍴ is APL's reshape. So if you want to reshape a flattened vector, you just need to do <new shape vector> ⍴ <vector to reshape> Commented Jun 17, 2019 at 16:04 • so could you use reshape to create the 2xN list and then convert each to integer? – mkst Commented Jun 17, 2019 at 17:10 • You could, but I don't think that'd be shorter than my current answer. One problem would be that, for my answer, reshaping the string into a 1117×2 matrix and then converting to integers would create a vector with 1117, one digit integers. See the difference between the method I'm using versus reshaping Commented Jun 17, 2019 at 18:11 • Ah, my flattened string gets reshaped into something more workable :) – mkst Commented Jun 17, 2019 at 18:56 # Charcoal, 12 bytes Ｉ⌕Ｉ⪪⭆φ×⁶⊕ι²Ｎ  Try it online! Link is to verbose version of code. 0-indexed. Explanation:  φ Predefined constant 1000 ⭆ Map over implicit range and join ι Current index ⊕ Incremented ×⁶ Multiplied by 6 ⪪ ² Split into pairs of digits Ｉ Cast to integer Ｎ Input as a number ⌕ Find its index Ｉ Cast to string Implicitly print  # J, 29 26 bytes -3 bytes thanks to FrownyFrog! i.~_2(".\;)6<@":@*1+i.@325  Try it online! 0-based • i.~_2(".\;)6<@":@*1+i.@325 Commented Jun 14, 2019 at 10:54 • @FrownyFrog Thank you! Nice use of boxing! Commented Jun 14, 2019 at 11:00 # Python 3, 87 81 Bytes: lambda n:[*zip(*[iter(''.join(map(str,range(6,1951,6))))]*2)].index((*'%02d'%n,))  integer input, 0-indexed output. Try it online! -6 bytes, thanks to @TFeld. # Wolfram Language (Mathematica), 74 bytes #&@@Position[FromDigits/@Flatten@IntegerDigits[6Range@365]~Partition~2,#]&  Try it online! 2 bytes saved from @Expired Data • You can get rid of one of the maps Commented Jun 14, 2019 at 13:36 • I was working on this before I saw yours.. merged them and got 77 bytes Commented Jun 14, 2019 at 13:41 • @ExpiredData nice. 74 bytes now Commented Jun 14, 2019 at 13:47 # Brachylog, 14 bytes ;İS∧⟦×₆ᵐcġ₂iSt  Try it online! Too slow for test cases with larger outputs. # R, 75 62 bytes -13 bytes thanks to Giuseppe. match(scan(,''),substring(Reduce(paste0,3*(a=2*1:999)),a-1,a))  Try it online! # x86-16 machine code, 54 53 bytes 00000000: bde5 032b e58b fc57 9633 c0b9 2701 0506 ...+...W.3..'... 00000010: 0050 e80d 0058 e2f6 965f 49f2 aff7 d103 .P...X..._I..... 00000020: e5c3 bb0a 0052 33d2 f7f3 85c0 7403 e8f4 .....R3.....t... 00000030: ff92 aa5a c3 ...Z.  Listing: BD 03E5 MOV BP, 997 ; digits list buffer size 2B E5 SUB SP, BP ; allocate from stack 8B FC MOV DI, SP ; DI = start of digits buffer 50 PUSH AX ; save input value 57 PUSH DI ; save buffer offset 33 C0 XOR AX, AX ; counter in AX = 0 B9 0127 MOV CX, 295 ; generate 295*6 multiples SIX_LOOP: 05 0006 ADD AX, 6 ; increment counter by 6 50 PUSH AX ; save AX (clobbered by OUT_DECU) E8 000D CALL OUT_DECU ; write AX to BCD at ES:DI 58 POP AX E2 F6 LOOP SIX_LOOP ; loop 295 times 5F POP DI ; restore start of output buffer 58 POP AX ; restore input number 49 DEC CX ; loop count FFFFh (solution guaranteed) F2 AF REPNZ SCASW ; search for input value F7 D1 NOT CX ; ones complement sign without increment 03 E5 ADD SP, BP ; restore stack pointer C3 RET OUT_DECU: ; divide unsigned word AX into base 10 ; decimal digits to buffer at ES:DI BB 000A MOV BX, 10 ; decimal divisor = 10 OUT_DECU_R: 52 PUSH DX ; save remainder digit / caller DX 33 D2 XOR DX, DX ; clear high word of dividend F7 F3 DIV BX ; AX = DX:AX / 10, DX = DX:AX % 10 85 C0 TEST AX, AX ; is zero? 74 03 JZ OUT_DECU_DONE ; loop while AX > 0 E8 FFF4 CALL OUT_DECU_R ; recursive call to next division OUT_DECU_DONE: 92 XCHG AX, DX ; AL = digit AA STOSB ; write to digits list 5A POP DX ; restore digit C3 RET  Callable function, input value as little endian BCD in AX. Output 1-based index in CX. The easy approach... generate the entire list and search it. Note: I found experimentally that 295*6 is sufficient to generate needed values: 295*6 = 1770 of which the 77 is the value for the highest index (497 or 498). This would produce a string 997 digits long. Tests: # MathGolf, 10 bytes •╒6*y░2/i=  Try it online! Basically the same as the 05AB1E answer, but I lose a byte by having to convert the concatenated number to string explicitly. ## Explanation •╒ 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  # APL+WIN, 37 bytes Prompts for input of integer. 1 indexed. ↑(⎕=⍎,((n,2)⍴(⍕6×⍳n)~' '),' ')/⍳n←600  Try it online! Coutesy of Dyalog Classic # C# (Visual C# Interactive Compiler), 123 bytes 115 bytes 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);  Try it online! • I think there's a bug in your solution somewhere, as f(61) should return 0 (it looks like your solution is 0-indexed) – Sok Commented Jun 14, 2019 at 9:38 • Thanks @sok should be fixed now Commented Jun 14, 2019 at 9:45 # K (oK), 22 bytes Solution: (.:'0N 2#,/$6*1+!999)?


Try it online!

Explanation:

0-indexed.

(.:'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


# Jelly, 10 bytes

ȷ×€6DFs2Ḍi


Try it online!

TIO link gives all values for 0 to 99.

### Explanation

ȷ          | 1000
×€6       | each times 6 (using implicit range from 1..1000)
D      | Convert to decimal digits
F     | Flatten
s2   | Split into pairs
Ḍ  | Convert back from decimal digits to integer
i | Find index of left argument to link


# Java 10, 119104 102 bytes

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


Port of @TFeld's Python 2 answer.
-2 bytes thanks to @Imus.

1-indexed.

Try it online.

Explanation:

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
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


Original 119 117 bytes version:

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-indexed.

Try it online.

Explanation:

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

• You can save 2 bytes by using ""+n/10+n%10 instead of n>9?n+"":"0"+n
– Imus
Commented Jun 17, 2019 at 9:29

# PHP, 58 bytes

<?=array_search($argn,str_split(join(range(6,1950,6)),2));  Try it online! Input is 0-based via STDIN. $ echo 0|php -F sixers.php
241

$echo 50|php -F sixers.php 131  # CJam, 17 bytes 325,:)6f*s2/:~ri#  Try it online! 0-based. ### Explanation 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  • Out of curiosity, why does CJam has builtins for all integers in the range $[10,20]$, as well as five different builtins that all default to an empty string "", but no builtins for $100$ or $1000$? Commented Jun 14, 2019 at 8:07 • @KevinCruijssen Not sure... But having variables with predefined values like 0 or "" is sometimes useful for loops, because those are often the desired starting values. As for not having 100 or 1000, yes, I agree they would more useful than say 18 or 19 Commented Jun 14, 2019 at 8:15 • It's a shame that leading zeroes are annoying, otherwise you could ditch the :~ and i from your code. :( Commented Jun 15, 2019 at 17:43 # Japt, 12 bytes 0-indexed. 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)  # Red, 97 94 bytes func[n][(index? find/skip rejoin collect[repeat i 325[keep i * 6]]pad/left/with n 2 #"0"2)/ 2]  Try it online! # Stax, 11 bytes Æ▒§±~>ûπ╞öt  Run and debug it Input is an integer. Output is 0-indexed. # Perl 5-MList::Util=any -ap, 50 bytes $\=0,$b.=6*++$,while!any{++$\;"@F"==$_}$b=~/../g}{  Try it online! 1-based output # Retina, 83 77 bytes I am really out of practice at complicated programming in Retina, but I'm satisfied with the length I managed to do it in. Outputs the 0-indexed result. .+ 6*1 325+-1%1+$0¶6*1$0 1+$.0
¶

L..
m^0

$¶$+
s\b(\d+)\b.*\b\1$C¶  Try it Online ### Explanation .+ 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

$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  # Swift 5/Xcode 10.2.1, 140134 133 bytes let s=sequence(first:6){$0+6}.lazy.map{String($0)}.joined();return(0...).first{$0%2==0&&Int(String(s.dropFirst($0).prefix(2)))==n}!/2  Try it online! # Retina 0.8.2, 36 bytes ^ 2406$*_
_{6}
$. ^0(..)+?.*\1$
$#1  Try it online! Link includes test suite. 1-indexed. Explanation: ^ 2406$*_


Prefix 2406 _s to the input.

_{6}
$.  Replace every 6 _s with the number of preceding _s. This generates the sequence 0, 6, 12 ... 2400, but automatically concatenates the numbers. ^0(..)+?.*\1$


Skip the leading 0 and find the first pair of digits that match the last two digits i.e. the zero-padded input (because the string ends in 0; in fact the test suite uses the fact that it ends in 00).

$#1  Output the number of pairs of digits up to and including the match. Retina 1 saves a couple of bytes because its string repetition operator is a byte shorter and already defaults to _ as its right-hand operand, so that the second line of code becomes just 2406*. Another feature of Retina 1 is the > modifier which generates the substitution in the context of the separator after the match, which in the case of $.> causes it to include the length of the match in the result. Although this costs a byte we save it immediately as we don't need to match the 0 any more. (The repetitions also have to be reduced by 6.) Retina 1 can also do basic arithmetic in a substitution. This means that we don't have to resort to tricks to take multiples of 6, instead we just generate the numbers 1..400 and multiply by 6 in the substitution. Remarkably, this also doesn't affect the overall byte count, as the final result looks like this:

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


# Bash, 80 bytes

1-indexed.

dc<<<6[p6+lax]salax|sed 's/./\n&/g;s/\n//'|sed 'N;s/\n//'|sed -n "/^0*$1$/{=;q}"


Try it online!

n=>{int i=2;for(var s="612";!s.StartsWith($"{n:d2}");s=s.Substring(2)+6*++i);return~-i;}  Try it online! Another port of the Java and Python answers. My original answer below: # C# (Visual C# Interactive Compiler), 102 bytes 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;}


Try it online!

Both solutions use 1-based indexing.