17
\$\begingroup\$

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.
  • Links to test your code online are recommended!
  • 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
\$\endgroup\$
  • 6
    \$\begingroup\$ It may be useful to know that considering 6, 2*6, 3*6,..., 325*6 is sufficient to generate all possible values \$\endgroup\$ – Luis Mendo Jun 14 at 7:37
  • \$\begingroup\$ @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) \$\endgroup\$ – Sok Jun 14 at 7:44
  • \$\begingroup\$ Can we take the input-integer as string, with the ones \$n<10\$ padded with a leading 0 (i.e. 00, 01, 02, ...)? \$\endgroup\$ – Kevin Cruijssen Jun 14 at 7:45
  • 10
    \$\begingroup\$ @KevinCruijssen Hmmm, input as a string is fine, but left padding with 0 is a bit too far IMO. \$\endgroup\$ – Sok Jun 14 at 7:51

31 Answers 31

12
\$\begingroup\$

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)
\$\endgroup\$
12
\$\begingroup\$

Python 2, 93 92 85 83 81 68 65 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
\$\endgroup\$
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – ArBo Jun 14 at 13:18
  • \$\begingroup\$ @ArBo Even better, f=lambda n,s='612',i=18:n-int(s[:2])and-~f(n,s[2:]+`i`,i+6) (0-indexed). \$\endgroup\$ – xnor Jun 15 at 1:07
  • \$\begingroup\$ @ArBo Thanks :) \$\endgroup\$ – TFeld Jun 15 at 7:16
  • \$\begingroup\$ @xnor Thanks :) \$\endgroup\$ – TFeld Jun 15 at 7:28
8
\$\begingroup\$

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
\$\endgroup\$
7
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 9 bytes

₄L6*J2ôIk

0-indexed. Accepts either a single integer, or a list of integers as input.

Try it online or verify all test cases.

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)
\$\endgroup\$
  • \$\begingroup\$ Is the default behavior to join as string, or are there separate operators for joining as string and joining as number? \$\endgroup\$ – maxb Jun 14 at 12:22
  • \$\begingroup\$ @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. \$\endgroup\$ – Kevin Cruijssen Jun 14 at 13:03
  • \$\begingroup\$ @maxb Relevant 05AB1E tip giving some additional examples. \$\endgroup\$ – Kevin Cruijssen Jun 14 at 13:10
4
\$\begingroup\$

Charcoal, 12 bytes

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

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
  I             Cast to integer
           N    Input as a number
 ⌕              Find its index
I               Cast to string
                Implicitly print
\$\endgroup\$
4
\$\begingroup\$

J, 29 26 bytes

-3 bytes thanks to FrownyFrog!

i.~_2(".\;)6<@":@*1+i.@325

Try it online!

0-based

\$\endgroup\$
  • 1
    \$\begingroup\$ i.~_2(".\;)6<@":@*1+i.@325 \$\endgroup\$ – FrownyFrog Jun 14 at 10:54
  • \$\begingroup\$ @FrownyFrog Thank you! Nice use of boxing! \$\endgroup\$ – Galen Ivanov Jun 14 at 11:00
4
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ can you reshape the flattened vector like you can in K? Google suggests but APL scares me... \$\endgroup\$ – streetster Jun 15 at 20:25
  • \$\begingroup\$ @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> \$\endgroup\$ – J. Sallé Jun 17 at 16:04
  • \$\begingroup\$ so could you use reshape to create the 2xN list and then convert each to integer? \$\endgroup\$ – streetster Jun 17 at 17:10
  • \$\begingroup\$ 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 \$\endgroup\$ – J. Sallé Jun 17 at 18:11
  • \$\begingroup\$ Ah, my flattened string gets reshaped into something more workable :) \$\endgroup\$ – streetster Jun 17 at 18:56
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 74 bytes

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

Try it online!

2 bytes saved from @Expired Data

\$\endgroup\$
3
\$\begingroup\$

Brachylog, 14 bytes

;İS∧⟦×₆ᵐcġ₂iSt

Try it online!

Too slow for test cases with larger outputs.

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ @Giuseppe Agreed, that looks legit. Thanks! \$\endgroup\$ – Robin Ryder Jun 26 at 21:04
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

APL+WIN, 37 bytes

Prompts for input of integer. 1 indexed.

↑(⎕=⍎,((n,2)⍴(⍕6×⍳n)~' '),' ')/⍳n←600

Try it online! Coutesy of Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ I think there's a bug in your solution somewhere, as f(61) should return 0 (it looks like your solution is 0-indexed) \$\endgroup\$ – Sok Jun 14 at 9:38
  • 1
    \$\begingroup\$ Thanks @sok should be fixed now \$\endgroup\$ – Expired Data Jun 14 at 9:45
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Java 10, 119 104 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
                                //   (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

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
\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 2 bytes by using ""+n/10+n%10 instead of n>9?n+"":"0"+n \$\endgroup\$ – Imus Jun 17 at 9:29
  • \$\begingroup\$ @Imus Thanks! :) \$\endgroup\$ – Kevin Cruijssen Jun 17 at 9:37
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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\$? \$\endgroup\$ – Kevin Cruijssen Jun 14 at 8:07
  • \$\begingroup\$ @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 \$\endgroup\$ – Luis Mendo Jun 14 at 8:15
  • 1
    \$\begingroup\$ It's a shame that leading zeroes are annoying, otherwise you could ditch the :~ and i from your code. :( \$\endgroup\$ – Erik the Outgolfer Jun 15 at 17:43
1
\$\begingroup\$

Japt, 12 bytes

0-indexed.

L²õ*6 ¬ò b¥U

Try it or test all inputs

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)
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Stax, 11 bytes

Æ▒§±~>ûπ╞öt

Run and debug it

Input is an integer. Output is 0-indexed.

\$\endgroup\$
1
\$\begingroup\$

Perl 5 -MList::Util=any -ap, 50 bytes

$\=0,$b.=6*++$,while!any{++$\;"@F"==$_}$b=~/../g}{

Try it online!

1-based output

\$\endgroup\$
1
\$\begingroup\$

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
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
\$\endgroup\$
1
\$\begingroup\$

Swift 5/Xcode 10.2.1, 140 134 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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 88 bytes

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.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 46 bytes

->n{[*6.step(5**5,6)].join.scan(/../).index n}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Clojure, 102 bytes

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

So long! :(

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.