# Kolakoski sequence

In mathematics, the Kolakoski sequence (named after William Kolakoski, who discussed it in 1965[1]) is an infinite sequence of symbols {1,2} which is its own run-length encoding.[2]

The initial terms of the sequence are:

1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,2,1,1,2,1,2,2,1,1,… (sequence A000002 in OEIS)

Each symbol occurs in a "run" of either 1 or 2 consecutive terms, and writing down the lengths of these runs gives exactly the same sequence. It is the unique sequence with this property except for the same sequence with the initial 1 deleted.[2]

## Algorithm

The sequence may be generated by an algorithm that, in the ith iteration, reads the value xi that has already been output as the ith value of the sequence (or, if no such value has been output yet, sets xi = i). Then, if i is odd, it outputs xi copies of the number 1, while if i is even, it outputs xi copies of the number 2. Thus, the first few steps of the algorithm are:

1. The first value has not yet been output, so set x1 = 1, and output 1 copy of the number 1
2. The second value has not yet been output, so set x2 = 2, and output 2 copies of the number 2
3. The third value x3 was output as 2 in the second step, so output 2 copies of the number 1.
4. The fourth value x4 was output as 1 in the third step, so output 1 copy of the number 2.

etc.

## Density

It seems plausible that the density of 1s is 1/2, but this conjecture remains unproved.[3] Chvátal has proved that the upper density of 1's is less than 0.50084.[4]

## History

Kolakoski's self-generating sequence has attracted the interest of computer scientists as well as mathematicians. For example, Stephen Wolfram describes the Kolakoski sequence in connection with the history of cyclic tag systems.[5]

Recently Jean Berstel and Srecko Brlek discovered that the Kolakoski sequence already appeared, before Kolakoski, in a 1939 paper of Rufus Oldenburger.