Math::N...olakoski(3)

πŸ“¦
version 75 Β· 2 versions
, in fedora 43

πŸ’– Math::N...olakoski(3) β€” the Kolakoski sequence, explained by your unix bestie πŸ’–

okay so bestie, we're in User Contributed Perl Documentation territory today and this page is literally about a sequence that's its own run lengths?? like it describes itself?? main character energy fr fr πŸ‘‘


✨ NAME ✨

Math::NumSeq::Kolakoski β€” a sequence of 1s and 2s that is its own run lengths πŸ¦‹


✨ SYNOPSIS ✨

use Math::NumSeq::Kolakoski;
my $seq = Math::NumSeq::Kolakoski->new;
my ($i, $value) = $seq->next;

✨ DESCRIPTION ✨

so the Kolakoski sequence goes like this:

i     = 1  2  3  4  5  6  7  8  9
value = 1, 2, 2, 1, 1, 2, 1, 2, 2, ...          (OEIS A000002)

the values are only ever 1 or 2, and β€” here's the iconic part β€” the run lengths of the 1s and the 2s are given by the sequence values themselves. it's self-referential. it's giving ouroboros. it's giving iykyk 🐍

            |-| |--|  |--| |-||-| |--|
             1, 2, 2, 1, 1, 2, 1, 2, 2, ...      values
run length   1    2     2   1  1   2    ...    lengths are values again

like. the lengths ARE the values. the values ARE the lengths. no cap. πŸ’…


✨ FUNCTIONS ✨

peep "FUNCTIONS" in Math::NumSeq for the behaviour that's common to literally all the sequence classes, bestie 🫢

  • $seq = Math::NumSeq::Kolakoski->new () β€” makes a brand new sequence object and hands it back to you. slay. ✨

✨ FORMULAS ✨

okay this is the part where it gets lowkey genius. you do NOT need to keep the entire sequence lying around. you don't even need to keep the portion between where i is up to and the values past that which those up to i induce. nope. nada. πŸ™ˆ

instead, the value at i is determined by the earlier value, which is determined by a yet earlier value, and on and on and on like a lil chain of gossip πŸ’¬ at each level you only need to keep a value and a pending count. that's it. that's the whole storage situation.

and the number of levels you end up needing? about log base 1.6 of the position i. cute. efficient. obsessed. 😌


✨ SEE ALSO ✨

  • Math::NumSeq
  • Math::NumSeq::GolombSequence

✨ HOME PAGE ✨

<http://user42.tuxfamily.org/math-numseq/index.html> 🏠


✨ LICENSE ✨

Copyright 2011, 2012, 2013, 2014, 2016, 2019, 2020, 2021 Kevin Ryde

Math-NumSeq is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-NumSeq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.


πŸ’€ friendly reminder that "without even the implied warranty of merchantability" is not a vibe, it's a legal vibe. respect it.

k bye bestie, this was perl v5.42.0, dated 2025-07-25, and it's the Math::N...olakoski(3) page β€” go read some sequences, you absolute icon πŸ‘‘πŸŒΈ

πŸ“¦ where this page comes from