Staaarter

Generate Kolakoski Sequence

Generates N terms of the Kolakoski sequence, a self-describing sequence over {1, 2} where the sequence of run lengths reproduces the sequence itself. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-08-05
By Staaarter Team
sequencescombinatorics

Overview

Introduction

The Kolakoski sequence is one of the simplest and best-known examples of a self-describing, or self-generating, sequence in mathematics.

This generator builds the sequence incrementally, using its own run-length structure to determine each new term, and outputs however many terms you request.

What Is Generate Kolakoski Sequence?

The Kolakoski sequence, cataloged as OEIS A000002, is an infinite sequence over the symbols 1 and 2 that describes its own run lengths: the sequence of consecutive-run lengths within the sequence equals the sequence itself.

It begins 1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, ..., and this self-referential property makes it a classic example studied in combinatorics on words.

How Generate Kolakoski Sequence Works

The tool starts with the seed values 1 and 2, then reads through the sequence built so far, treating each of its terms as an instruction for how long the next run in the output should be.

It alternates the value being appended between 1 and 2 for each new run, and the length of that run is read directly from the current position in the sequence, which is what makes the construction self-describing.

When To Use Generate Kolakoski Sequence

Use it to explore self-describing sequences in combinatorics, or alongside Gijswijt's Sequence Generator and Look-and-Say Sequence Generator when studying this broader family.

It's also useful as a source of verified reference terms if you're implementing or testing your own Kolakoski sequence generator.

Features

Advantages

  • Builds the sequence directly and efficiently, without brute-force searching, so even large term counts generate almost instantly.
  • Produces the standard, well-documented version of the sequence, matching the canonical OEIS A000002 listing.
  • Simple comma-separated output that makes the alternating 1s and 2s, and their self-referential run lengths, easy to inspect visually.

Limitations

  • Capped at 5000 terms, mainly to keep the output a manageable size to read and copy rather than any computational constraint.
  • Only generates the classic {1, 2} alphabet version; it doesn't support alternate-alphabet Kolakoski-style variants sometimes discussed in the literature.

Examples

First 16 terms of the Kolakoski sequence

Input

16

Output

1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1

Reading the run lengths of this sequence (1, 2, 2, 1, 1, 2, ...) reproduces the sequence itself.

Best Practices & Notes

Best Practices

  • Generate at least 15-20 terms if you want to visually verify the self-describing property by comparing the sequence to its own run-length breakdown.
  • Use a larger term count, a few hundred or more, if you're studying the sequence's long-run density, which is conjectured but not proven to approach 1/2.

Developer Notes

The construction uses a read pointer that walks through the sequence being built, treating `seq[readIndex]` as the length of the next run to append, alternating the appended value between 1 and 2, and special-casing the very first run since it starts one element short due to the initial seed value.

Generate Kolakoski Sequence Use Cases

  • Studying self-describing sequences and combinatorics on words
  • Generating verified reference terms to test a custom Kolakoski sequence implementation
  • Exploring open questions about the sequence's long-run density and structure

Common Mistakes

  • Expecting a closed-form formula for the nth term, none is known, the sequence can only be built incrementally from its own structure.
  • Mixing up this sequence with Look-and-Say, they're both self-referential but built with entirely different rules, Look-and-Say encodes runs as digit pairs rather than reusing them as run lengths.

Tips

  • Manually compute the run-length breakdown of a short output and compare it to the sequence itself to see the self-describing property firsthand.
  • If you're comparing self-describing sequences side by side, generate matching term counts from Gijswijt's Sequence Generator too, since both use very different definitions of self-reference.

References

Frequently Asked Questions