expand(r) = { my (x = vector(vecsum(r)), d = 1, i = 0); for (j = 1, #r, for (k = 1, r[j], x[i++] = d; ); d = 3-d; ); return (x); } \\ A007782 Number of factors in the infinite word formed by the Kolakoski sequence A000002. len = [ /* 1, */ 2, 4, 6, 10, 14, 18, 26, 34, 42, 50, 62, 78, 94, 110, 126, 142, 162, 186, 218, 250, 282, 314, 346, 378, 410, 446, 486, 534, 590, 654, 718, 782, 846, 910, 974, 1038, 1102, 1166, 1234, 1302, 1378, 1458, 1554, 1658, 1774, 1898, 2026, 2154, 2282, 2410, 2538, 2666 ] \\ A000002 Kolakoski sequence: a(n) is length of n-th run; a(1) = 1; sequence consists just of 1's and 2's. kol = [ 1, 2 ] { m = 0; for (w = 1, #len, while (1, ww = Set(vector(#kol+1-w, k, fromdigits(kol[k..k+w-1]))); if (#ww == len[w], break, kol = expand(kol); ); ); apply (v -> print (m++ " " v), ww); if (m >= 10 000, break; ); ); } quit