base = 10

\\ numbers obtained by replacing a number (without leading zeros)
\\ appearing in the expansion of n by one of its divisors.
rr = [0]
row(n) = {
	my (d = digits(n, base), nb = 1);
	rr[1] = n;
	for (i = 1, #d,
		if (d[i],
			for (j = i, #d,
				my (x = fromdigits(d[i..j], base));
				fordiv (x, t,
					my (y = fromdigits(concat([
						d[1..i-1],
						digits(t, base),
						d[j+1..#d]
					]), base));

					if (nb++ > #rr,
						rr = concat(rr, vector(#rr));
					);

					rr[nb] = y;
				);
			);
		);
	);

	return (Set(rr[1..nb]))
}

{
	for (n = 1, #a = vector(10 000),
		if (n==1,
				a[n] = 1,

				r = row(n);
				a[n] = sum(k = 1, #r-1, a[r[k]]);
		);

		print (n " " a[n]);
	);
}

quit