BASE = 5;

\\ see/seen predicates
big = 1 000 000
s = 0
S = Set([])
unseen = 1
seen(v) = if (v < big, bit test(s, v), set search(S, v))
see(v) = if (v < big, s = bit or(s, 2^v), S = set union(S, Set([v]))); while (seen(unseen), unseen++)

signed key(n) = if (n>=0, 2*n, 2*abs(n)-1)

\\ is pandigital ?
is pan(n) = #vecsort(digits(n,BASE),,8)==BASE

\\ pandigital numbers, in increasing order
pans = []
unexplored = from digits(vector(BASE, i, if (i==1, i, i==2, 0, i-1)), BASE)

get pan(n) = {
	while (#pans < n,
		if (is pan(unexplored),
			pans = concat(pans, unexplored);
		);
		unexplored++;
	);
	return (pans[n]);
}

first pan = 1

\\ other term (from previous)
other(p) = {
	see(p); 
	while (get pan(first pan) < unseen + min(p, unseen),
		first pan++
	);
	for (n=first pan, oo,
		my (o = get pan(n) - p);
		if (o>0 && !seen(o),
			return (o);
		);
	);
}

{
	for (n=1, 10 000,
		a = if (n==1, 1, other(a));

		print (n " " a);
	);
}

quit