allocate mem(2^30) \\ sequence of 1's and 2's with given run lengths 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, 2794, 2922, 3050, 3178, 3306, 3434, 3566, 3702, 3838, 3982, 4134, 4302, 4478, 4666, 4870, 5090, 5330, 5570, 5822, 6078, 6334, 6590, 6846, 7102, 7358, 7614, 7870, 8126, 8382, 8638, 8894, 9150, 9406, 9662, 9918, 10174, 10434, 10698, 10970, 11242, 11522, 11802, 12098, 12402, 12742, 13086, 13442, 13810 ] \\ 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 ] rev(n,base=10) = fromdigits(Vecrev(digits(n,base)),base) { 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 -> if (v==rev(v), print (m++ " " v)), ww); ); } quit