s = 0 unseen = 1 seen(v) = bit test(s, v) see(v) = s = bit or(s, 2^v); while (seen(unseen), unseen++) alternatesumdigits(n, b) = my (s=0, u=+1); while (n, s+=u*(n%b); u=-u; n\=b); s other(p) = { see(p); my (h=alternatesumdigits(p, 2)); for (v=unseen, oo, if (!seen(v) && h <= alternatesumdigits(v, 3), return (v); ); ); } v=1; for (n=1, 25 000, print (n " " v); v=other(v)); quit