first(n) = {my(c = 1, res = vector(n), t = 0); while(t < n, if(is(c), t++; res[t] = c ); c = nxt(c); ); res } addhelp(is, "Given a palindrome, see if it's a term.") is(n) = my(f = factor(n)[, 2]); sum(i = 1, #f~, f[i]%2 == 1) <= 1 nxt(n)={my(d=digits(n)); i=(#d+1)\2; while(i&&d[i]==9, d[i]=0; d[#d+1-i]=0; i--); if(i, d[i]++; d[#d+1-i]=d[i], d=vector(#d+1); d[1]=d[#d]=1); sum(i=1, #d, 10^(#d-i)*d[i])} symmetric(n) = {if(n == 1, return("1")); my(f = factor(n), v=vector(bigomega(f)), t = 0); f = f~; f = vecsort(f, (x, y) -> 1 - (x[2]%2 - y[2]%2), 4); for(i = 1, #f, for(j = 1, f[2, i] >> 1, t++; v[t] = f[1, i]; v[#v~ + 1 - t] = f[1, i] ) ); if(#v % 2 == 1, v[#v \ 2 + 1] = f[1, #f]); res = vector(#v << 1); res[1] = ""; for(i = 1, #v, res[2*i] = v[i]; ); for(i = 1, #v - 1, res[2 * i + 1] = " * "; ); concat(res); } printSymmetries(n) = {my(v = first(n), spaces = ["", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]); for(i = 1, #v, print(i" "spaces[6-#digits(i)]" "v[i]" "spaces[11-#digits(v[i])]" "symmetric(v[i])))}