toruns(n) = { my (r=[]); while (n, my (v=valuation(n+n%2, 2)); n\=2^v; r=concat(v, r)); r } fromruns(r) = { my (v=0); for (k=1, #r, v=(v+k%2)*2^r[k]-k%2); v } vv = [0] nb = 0 mx = 64 \\ max number of bits explore(r, d) = { if (vecsum(r)<=mx, if (#vv<nb++, vv=concat(vv, vector(#vv)); ); vv[nb]=fromruns(r); if (r[#r]+d>0, explore(concat(r, r[#r]+d), d); ); ); } for (i=1, mx, for (d=-mx, +mx, explore([i], d))) n = 0 apply (v -> apply (r -> print (n++ " " r), toruns(v)), Set(vv[1..nb])) quit