upto(n) = {my(d = [1,1], res = List(), vs, ds); while(1, vs = vecsum(d); if(vs % 2 == 0 && !hassum(freqdigits(d), vs/2) && vecsort(d) == d, ds = fromdigits(d); if(ds > n, return(res)); listput(res, ds); ); d = digits(fromdigits(d) + 1); ) } addhelp(hassum, "See if some sum of elements listed by index in v give sum n. v[i] gives the frequency of a number i. v might be found with function freqdigits."); hassum(v, n, {l = 1}) = {my(cv); if(n == 0, return(1)); for(i = l, #v, if(i > n, next(1)); cv = v; if(cv[i] > 0, cv[i]--; if(hassum(cv, n - i, i) == 1, return(1)); ) ) } addhelp(freqdigits, "give a vector with 9 elements res[i] where res[i] gives the frequecy of digit i in n, ignoring 0's. (adapted to have input of vector with digits.)"); freqdigits(d) = {my( res = List(vector(10))); \\ d = digits(n), for(i = 1, #d, res[d[i]+1]++; ); listpop(res, 1); res }