addhelp(first, "The first n terms of the sequence."); first(n) = {my(res = vector(n-1), v = [1]); n--; while(1, t = 0; c = fromdigits(Vec(v)); if(c <= n, res[c] = t; t++; forperm(v, p, c = fromdigits(Vec(p)); if(c <= n, res[c] = t; t++; , next(1); ) ) , return(concat(1, res)); ); v = nxtA179239(v) ) } addhelp(nxtA179239, "A179239 lists numbers being the least number having it's digits. Given A179239(n), returns A179239(n+1).") nxtA179239(d,{b=10}) = { i = #d; while(i>0&&d[i]==b-1,i--); if(i>1, if(d[i]>0, d[i]++ , d[i]=d[1] );for(j=i+1,#d,d[j]=d[i]) , if(i==1, d[i]++;for(j=2,#d,d[j]=0) , return(digits(10^(#d))) ) ); d }