\\ describe a fraction: \\ - integer part \\ - transient part \\ - periodic part fracdigits(v, base) = { \\ integer part my (int=digits(floor(v), base)); \\ period length my (den=denominator(v), pr=factor(base)[,1]~, p=znorder(Mod(base, den / prod(i=1, #pr, pr[i]^valuation(den,pr[i]))))); \\ transient part my (tra=[], fra=frac(v)); while (fra != frac(fra*base^p), tra = concat(tra, floor(fra*base)); fra = frac(fra*base); ); \\ periodic part my (per = vector(p, x, my (d=floor(fra*base)); fra = frac(fra*base); d)); return ([int,tra,per]); } \\ rebuild a fraction fromfracdigits(f, base) = fromdigits(f[1],base) + ( fromdigits(f[2],base) + fromdigits(f[3],base)/base^#f[3] / (1-1/base^#f[3])) / base^#f[2]; c(n) = { my (f=fracdigits(n, 3), erase=0); for (i=1, #f, for (j=1, #f[i], if (erase, f[i][j] = 0, f[i][j]==1, erase = 1, f[i][j]==2, f[i][j] = 1); ); ); if (erase, f[2] = concat(f[2],f[3]); f[3] = [0]; ); return (fromfracdigits(f,2)); } for (n=1, 3^logint(10 000,3), print (n " " numerator(c(1/n)))) quit