allocatemem() /* first number with binary weight n */ f(n) = 2^n-1 /* next number with same binary weight */ g(n) = \ my (r=0, p=0, d); \ while (1, \ d = n%2; \ if (d==0 && r>0, \ return ((n+1)*2^p + f(r-1)) \ ); \ n = n\2; \ p = p+1; \ r = r+d \ ) { a = vector(10 000); o = vector(#a,i,f(i)); for (n=1, #a, b=1; fordiv(n, d, if (o[d] < o[b], b=d) ); a[n] = o[b]; o[b] = g(o[b]); print(n" "a[n]) ) } quit