allocate mem(2^30); bits(n) = { my (b=vector(hammingweight(n))); for (k=1, #b, n -= 2^(b[k] = valuation(n, 2)); ); b } o = select(v -> hammingweight(v)%2, [0..M=2^16]) cache = apply(bits, [0..M]) bits(n) = cache[1+n] T(n,k) = { my (nn=bits(o[n]), kk=bits(o[k]), v=0); for (i=1, #nn, for (j=1, #kk, v=bitxor(v, 2^bitxor(nn[i], kk[j])); ); ); my (w=setsearch(o, v)); if (w, return (w), quit); } for (n=1, 2^11, print (n " " v=T(n, 3))) quit