A000002 Kolakoski sequence: a(n) is length of n-th run; a(1) = 1; sequence consists just of 1's and 2's.
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, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1, 1, 2, 2, 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
Offset: 1
Examples
Start with a(1) = 1. By definition of the sequence, this says that the first run has length 1, so it must be a single 1, and a(2) = 2. Thus, the second run (which starts with this 2) must have length 2, so the third term must be also be a(3) = 2, and the fourth term can't be a 2, so must be a(4) = 1. Since a(3) = 2, the third run must have length 2, so we deduce a(5) = 1, a(6) = 2, and so on. The correction I made was to change a(4) to a(5) and a(5) to a(6). - _Labos Elemer_, corrected by _Graeme McRae_
References
- Jean-Paul Allouche and Jeffrey Shallit, Automatic Sequences, Cambridge Univ. Press, 2003, p. 337.
- Éric Angelini, "Jeux de suites", in Dossier Pour La Science, pp. 32-35, Volume 59 (Jeux math'), April/June 2008, Paris.
- F. M. Dekking, What Is the Long Range Order in the Kolakoski Sequence?, in The mathematics of long-range aperiodic order (Waterloo, ON, 1995), 115-125, NATO Adv. Sci. Inst. Ser. C Math. Phys. Sci., 489, Kluwer Acad. Publ., Dordrecht, 1997. Math. Rev. 98g:11022.
- Michael S. Keane, Ergodic theory and subshifts of finite type, Chap. 2 of T. Bedford et al., eds., Ergodic Theory, Symbolic Dynamics and Hyperbolic Spaces, Oxford, 1991, esp. p. 50.
- J. C. Lagarias, Number Theory and Dynamical Systems, pp. 35-72 of S. A. Burr, ed., The Unreasonable Effectiveness of Number Theory, Proc. Sympos. Appl. Math., 46 (1992). Amer. Math. Soc.
- Michel Rigo, Formal Languages, Automata and Numeration Systems, 2 vols., Wiley, 2014. Mentions this sequence - see "List of Sequences" in Vol. 2.
- N. J. A. Sloane, A Handbook of Integer Sequences, Academic Press, 1973 (includes this sequence).
- N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
- Ilan Vardi, Computational Recreations in Mathematica. Addison-Wesley, Redwood City, CA, 1991, p. 233.
Links
- N. J. A. Sloane, Table of n, a(n) for n = 1..10502
- Jean-Paul Allouche, Michael Baake, Julien Cassaigne and David Damanik, Palindrome complexity, arXiv:math/0106121 [math.CO], 2001; Theoretical Computer Science, Vol. 292 (2003), pp. 9-31.
- Michael Baake and Bernd Sing, Kolakoski-(3,1) is a (deformed) model set, arXiv:math/0206098 [math.MG], 2002-2003.
- Alex Bellos and Brady Haran, The Kolakoski Sequence, Numberphile video (2017).
- Olivier Bordellès and Benoit Cloitre, Bounds for the Kolakoski Sequence, J. Integer Sequences, Vol. 14 (2011), Article 11.2.1.
- Richard P. Brent, Fast algorithms for the Kolakoski sequence, Slides from a talk, 2016.
- Éric Brier, Rémi Géraud-Stewart, David Naccache, Alessandro Pacco, and Emanuele Troiani, The Look-and-Say The Biggest Sequence Eventually Cycles, arXiv:2006.07246 [math.DS], 2020.
- Arturo Carpi, On repeated factors in C^infinity-words, Information Processing Letters, Vol. 52 (1994), pp. 289-294.
- Benoit Cloitre, The Kolakoski transform of words.
- Benoit Cloitre, Plot of walk on the first 60000 terms (step of unit length moving right with angle Pi/2 if 2 and left with angle -Pi/2 if 1 starting at (0,0)).
- F. M. Dekking, Regularity and irregularity of sequences generated by automata, Seminar on Number Theory, 1979-1980 (Talence, 1979-1980), Exp. No. 9, 10 pp., Univ. Bordeaux I, Talence, 1980.
- F. M. Dekking, On the structure of self-generating sequences, Seminar on Number Theory, 1980-1981 (Talence, 1980-1981), Exp. No. 31, 6 pp., Univ. Bordeaux I, Talence, 1981. Math. Rev. 83e:10075. JSTOR
- F. M. Dekking, What Is the Long Range Order in the Kolakoski Sequence?, Report 95-100, Technische Universiteit Delft, 1995.
- F. M. Dekking, The Thue-Morse Sequence in Base 3/2, J. Int. Seq., Vol. 26 (2023), Article 23.2.3.
- Jörg Endrullis, Dimitri Hendriks and Jan Willem Klop, Degrees of Streams, Integers, Vol. 11B (2011), A6.
- David Eppstein, The Kolakoski tree and The Kolakoski sequence via bit tricks instead of recursion.
- Jean-Marc Fédou and Gabriele Fici, Some remarks on differentiable sequences and recursivity, Journal of Integer Sequences, Vol. 13 (2010), Article 10.3.2.
- Abdallah Hammam, Some new Formulas for the Kolakoski Sequence A000002, Turkish Journal of Analysis and Number Theory, Vol. 4, No. 3 (2016), pp. 54-59.
- Mari Huova and Juhani Karhumäki, On Unavoidability of k-abelian Squares in Pure Morphic Words, Journal of Integer Sequences, Vol. 16 (2013), #13.2.9.
- Clark Kimberling, Integer Sequences and Arrays, Illustration of the Kolakoski sequence.
- William Kolakoski, Problem 5304, Amer. Math. Monthly, Vol. 72, No. 8 (1965), p. 674; Self Generating Runs, Solution to Problem 5304 by Necdet Üçoluk, Vol. 73, No. 6 (1966), pp. 681-682.
- Leonid V. Kovalev, Kolakoski sequence II.
- Elizabeth J. Kupin and Eric S. Rowland, Bounds on the frequency of 1 in the Kolakoski word, arXiv:0809.2776 [math.CO], Sep 16, 2008.
- Rabie A. Mahmoud, Hardware Implementation of Binary Kolakoski Sequence, Research Gate, 2015.
- Kerry Mitchell, Spirolateral-Type Images from Integer Sequences, 2013.
- Johan Nilsson, A Space Efficient Algorithm for the Calculation of the Digit Distribution in the Kolakoski Sequence, J. Int. Seq., Vol. 15 (2012), Article 12.6.7; arXiv preprint, arXiv:1110.4228 [math.CO], Oct 19, 2011.
- J. Nilsson, Letter Frequencies in the Kolakoski Sequence, Acta Physica Polonica A, Vol. 126 (2014), pp. 549-552.
- Rufus Oldenburger, Exponent trajectories in symbolic dynamics, Trans. Amer. Math. Soc., Vol. 46 (1939), pp. 453-466.
- Matthew Parker, The first 50K terms (7-Zip compressed file).
- Gheorghe Păun and Arto Salomaa, Self-reading sequences, Amer. Math. Monthly, Vol. 103, No. 2 (1996), pp. 166-168.
- Michael Rao, Trucs et bidules sur la séquence de Kolakoski, 2012, in French.
- A. Scolnicov, Kolakoski sequence, PlanetMath.org.
- Bobby Shen, A uniformness conjecture of the Kolakoski sequence, graph connectivity, and correlations, arXiv:1703.00180 [math.CO], 2017.
- Bernd Sing, More Kolakoski Sequences, INTEGERS, Vol. 11B (2011), A14.
- N. J. A. Sloane, Handwritten notes on Self-Generating Sequences, 1970. (note that A1148 has now become A005282)
- N. J. A. Sloane, Coordination Sequences, Planing Numbers, and Other Recent Sequences (II), Experimental Mathematics Seminar, Rutgers University, Jan 31 2019, Part I, Part 2, Slides. (Mentions this sequence)
- Bertran Steinsky, A Recursive Formula for the Kolakoski Sequence A000002, J. Integer Sequences, Vol. 9 (2006), Article 06.3.7.
- Eric Weisstein's World of Mathematics, Kolakoski Sequence
- Wikipedia, Kolakoski sequence.
- Gus Wiseman, Kolakoski fractal animation for n=40000.
- Ed Wynn, Program to generate A000002 in Shakespeare Programming Language.
- Index entries for "core" sequences
Crossrefs
Cf. A001083, A006928, A042942, A069864, A010060, A078929, A171899, A054353 (partial sums), A074286, A216345, A294447.
Cf. A118270.
Kolakoski-type sequences using other seeds than (1,2):
A078880 (2,1), A064353 (1,3), A071820 (2,3), A074804 (3,2), A071907 (1,4), A071928 (2,4), A071942 (3,4), A074803 (4,2), A079729 (1,2,3), A079730 (1,2,3,4).
Cf. A088568 (partial sums of [3 - 2 * a(n)]).
Programs
-
Haskell
a = 1:2: drop 2 (concat . zipWith replicate a . cycle $ [1,2]) -- John Tromp, Apr 09 2011
-
Maple
M := 100; s := [ 1,2,2 ]; for n from 3 to M do for i from 1 to s[ n ] do s := [ op(s),1+((n-1)mod 2) ]; od: od: s; A000002 := n->s[n]; # alternative implementation based on the Cloitre formula: A000002 := proc(n) local ksu,k ; option remember; if n = 1 then 1; elif n <=3 then 2; else for k from 1 do ksu := add(procname(i),i=1..k) ; if n = ksu then return (3+(-1)^k)/2 ; elif n = ksu+ 1 then return (3-(-1)^k)/2 ; end if; end do: end if; end proc: # R. J. Mathar, Nov 15 2014
-
Mathematica
a[steps_] := Module[{a = {1, 2, 2}}, Do[a = Append[a, 1 + Mod[(n - 1), 2]], {n, 3, steps}, {i, a[[n]]}]; a] a[ n_] := If[ n < 3, Max[ 0, n], Module[ {an = {1, 2, 2}, m = 3}, While[ Length[ an] < n, an = Join[ an, Table[ Mod[m, 2, 1], { an[[ m]]} ]]; m++]; an[[n]]]] (* Michael Somos, Jul 11 2011 *) n=8; Prepend[ Nest[ Flatten[ Partition[#, 2] /. {{2, 2} -> {2, 2, 1, 1}, {2, 1} -> {2, 2, 1}, {1, 2} -> {2, 1, 1}, {1, 1} -> {2, 1}}] &, {2, 2}, n], 1] (* Birkas Gyorgy, Jul 10 2012 *) KolakoskiSeq[n_Integer] := Block[{a = {1, 2, 2}}, Fold[Join[#1, ConstantArray[Mod[#2, 2, 1], #1[[#2]]]] &, a, Range[3, n]]]; KolakoskiSeq[999] (* Mikk Heidemaa, Nov 01 2024 *) (* Corrected by Giorgos Kalogeropoulos, May 09 2025 *)
-
PARI
my(a=[1,2,2]); for(n=3,80, for(i=1,a[n],a=concat(a,2-n%2))); a
-
PARI
{a(n) = local(an=[1, 2, 2], m=3); if( n<1, 0, while( #an < n, an = concat( an, vector(an[m], i, 2-m%2)); m++); an[n])};
-
Python
# For explanation see link. def Kolakoski(): x = y = -1 while True: yield [2,1][x&1] f = y &~ (y+1) x ^= f y = (y+1) | (f & (x>>1)) K = Kolakoski() print([next(K) for in range(100)]) # _David Eppstein, Oct 15 2016
Formula
These two formulas define completely the sequence: a(1)=1, a(2)=2, a(a(1) + a(2) + ... + a(k)) = (3 + (-1)^k)/2 and a(a(1) + a(2) + ... + a(k) + 1) = (3 - (-1)^k)/2. - Benoit Cloitre, Oct 06 2003
a(n+2)*a(n+1)*a(n)/2 = a(n+2) + a(n+1) + a(n) - 3 (this formula doesn't define the sequence, it is just a consequence of the definition). - Benoit Cloitre, Nov 17 2003
a(n+1) = 3 - a(n) + (a(n) - a(n-1))*(a(b(n)) - 1), where b(n) is the sequence A156253. - Jean-Marc Fedou and Gabriele Fici, Mar 18 2010
a(n) = (3 + (-1)^A156253(n))/2. - Benoit Cloitre, Sep 17 2013
Conjectures from Boštjan Gec, Oct 07 2024: (Start)
a(n)*(a(n-1) + a(n-2) - 3) + a(n-1)*a(n-2) + 7 = 3*a(n-1) + 3*a(n-2).
a(n)*(a(n-1) + a(n-2) - 3) = a(n-3)*(a(n-1) + a(n-2) - 3). (End)
Comment from Kevin Ryde, Oct 07 2024: The above formulas are true: The parts identify when terms are same or different and they hold for any sequence of 1's and 2's with run lengths 1 or 2.
Extensions
Minor edits to example and PARI code made by M. F. Hasler, May 07 2014
Comments