# Base-k representations of the co-domain of a polynomial - is it context-free?

In chapter 4 of Jeffrey Shallit's A Second Course in Automata Theory the following problem is listed as open:

Let $$p(n)$$ be a polynomial with rational coefficients such that $$p(n) \in \mathbb{N}$$ for all $$n \in \mathbb{N}$$. Prove or disprove that the language of the base-k representations of all integers in $$\{p(n) \mid n \geqslant 0\}$$ is context-free if and only if the degree of $$p$$ is $$\leqslant 1$$.

What's its status now (as for Oct 2018)? Is it proven? What about some special cases?

• If $k = 1$ (unary representation) then even the simplest $p(n) = n^2$ is not context-free (the well known non CF language $L = \{ 1^{n^2} \}$) – Marzio De Biasi Nov 9 '18 at 16:27
• @MarzioDeBiasi The so-called unary representation is not base-$1$. The only integer representable in actual base-$1$ would be $0$. – Emil Jeřábek Nov 19 '18 at 8:07
• @EmilJeřábek: I think that in many contexts base-1 is an alias of "unary representation". – Marzio De Biasi Nov 20 '18 at 11:25

Of course $$k \geq 2$$ here.

There once was a manuscript by Horváth that claimed to solve the problem, but it was unclear in several places and to my knowledge was never published.

As far as I know, the problem is still open. One direction of the implication is easy, of course.

• Is it already solved for $k= 2$? (I have an idea to prove it for $k=2$ and if it works the same technique could probably be applied to other bases) – Marzio De Biasi Nov 10 '18 at 19:50
• I would be very happy to hear any feedback from you about my answer. – domotorp Dec 6 '18 at 12:22
• I can't understand your claimed solution, sorry. – Jeffrey Shallit Dec 7 '18 at 0:04
• I've posted another answer with a lot more detail; the full statement is quite complex, so I've added some simpler lemmas before it that have the main idea, hopefully that makes the whole thing more believable. – domotorp Dec 15 '18 at 21:44

This is a sketch of the proof for $$k=2$$ and $$L = \{[n^2]_2 \mid n \geq 1\}$$; where $$[n^2]_2$$ is the binary representation of $$n^2$$. For better clarity we place the least significant bit of the binary strings on the left, e.g. $$[4^2]_2 = 00001$$.

The core idea is to assume that $$L$$ is context free, and then try to "simplify" it intersecting it with a simple regular language $$R$$; the new language $$L \cap R$$ is still context free and it still should contain binary representations of squares; then we apply the pumping lemma for CF languages in order to get a binary string that is not a representation of a square.

Intersecting $$L$$ with regular words that only contains a small finite number of $$1$$ digits isn't promising. It turns out that up to four $$1$$ digits $$(R = \{ 0^*1 \}, \{ 0^*10^*1 \}, \{ 0^*10^*10^*1 \}, \{0^*10^*10^*10^*1\} )$$ we get a CF language; and with five $$1$$ digits we get an apparently difficult number theory problem.

The promising approach is to intersect $$L$$ with $$R = 1\,0^+\,1^+\,0^+\,1$$; that is equivalent to restrict $$L$$ to the squares:

$$n^2 = 2^0 + 2^a((2^b - 1) + 2^{b+c}), 1

(informally odd squares whose binary representation contains all $$0$$s except a sequence of $$1$$s in the middle).

    n        n^2  n                  n^2
39       1521  111..1             1...11111.1
143      20449  1111...1           1....1111111..1
543     294849  11111....1         1.....111111111...1
2111    4456321  111111.....1       1......11111111111....1
8319   69205761  1111111......1     1.......1111111111111.....1
33023 1090518529  11111111.......1   1........111111111111111......1
LSB          MSB   LSB                         MSB


With some efforts, we can prove the following:

Theorem: the number $$2^0 + 2^a((2^b - 1) + 2^{b+c});\quad 0 is a square if and only if

$$b = 2a-3, c = a - 3$$

(the proof is quite long, I'll publish it on my blog)

At this point, we can easily prove that $$L \cap R$$ is not context free using the pumping lemma (we can pump at most two "segments" of the $$100..0011...1100.001$$ string). So also $$L$$ is not context free.

Probably the same technique can be applied to any base $$k$$.

• The result for $(n^2)$ in base 2 has been known for a long time. The challenge is to make a construction like this work for every polynomial that maps integers to integers, and for every base. – Jeffrey Shallit Nov 18 '18 at 10:07
• We are so similar, I've also spent my last couple of days thinking about this problem, though I've taken a completely different approach. – domotorp Nov 18 '18 at 21:28

I think I have a proof. The proof follows from this lemma.

Lemma. For a context-free language $$L$$ if for infinitely many $$n$$ there are $$n^6$$ words of equal length whose first $$n^2$$ letters are the same and their last $$n$$ letters are different (pairwise), then there is a $$B$$ such that there are infinitely many pairs $$u,v\in L$$ of equal length that differ only in their last $$B$$ letters.

So if $$u$$ and $$v$$ represent binary numbers, then their difference will be at most $$2^B$$ infinitely often, which is impossible for polynomials. On the other hand, with some number theory it can be shown that the condition is satisfied for every integer valued polynomial $$p$$: Take any $$x_1,\ldots,x_{n^6}$$ for which $$f(x_i)\ne f(x_j)$$, and then add some sufficiently large number $$N$$ to each of them to obtain the desired words $$f(x_i+N)$$.

Proof of the lemma. Take a large enough $$n$$ such that there are $$n^6$$ words of equal length, $$w_1,\ldots,w_{n^6}$$, that satisfy the conditions. For each $$w_i$$ fix a way in which it can be generated from the context-free grammar. (Warning! I'm not an expert of this field, so I might not use the proper terms.)

Say that the application of a rule $$A\to BC$$ splits two letters $$b$$ and $$c$$ of the final word, if the $$b$$ and $$c$$ are both derived from $$A$$, but $$b$$ is derived from $$B$$, while $$c$$ is derived from $$C$$. Each rule splits at most $$O(1)$$ letters of $$w_i$$ from each other.

In any $$w_i$$, there will be $$\Omega(n)$$ consecutive letters among the first $$n^2$$ letters that are split from each other by some consecutive rules such that no two letters among the last $$n$$ letters are split from each other while applying these rules. If we write these rules collectively for letter $$w_i$$ as $$A_i\to B_i^1B_i^2\ldots B_i^n$$, then no letter from the last $$n$$ letters is derived from $$B_i^j$$ for $$j, and $$B_i^1B_i^2\ldots B_i^{n-1}$$ are all converted into some part of the first $$n^2$$ letters. We can apply the pumping lemma to the rule $$A_i\to B_i^1B_i^2\ldots B_i^n$$ if $$n$$ is large enough.

There are only $$\binom{n^2}2$$ choices for the interval of $$\Omega(n)$$ letters, $$O(n)$$ options about what the pumping lemma gives (as it has $$O(1)$$ length), so by the pigeonhole principle there will be two words for which these are all the same. But then after pumping we can obtain an arbitrarily long common initial part for these two words, while we know that they'll differ only in their last $$n$$ bits.

Note. This is a much more detailed version of my other answer, as that didn't seem to be comprehensible enough. I've tried to convert it to resemble more standard pumping lemmas, but the full proof got way to complex. I recommend to read the statement of the first two lemmas to understand the main idea, then the statement of the Corollary, and finally the end, where I prove why the Corollary implies the answer to the question.

The proof is based on a generalization of the pumping lemma. The lemma that we need is quite elaborate, so instead of stating it right away, I start with some easier generalizations, eventually building up to more complicated ones. As I've later learned, this is very similar to the so-called interchange lemma.

Twin Pumping Lemma. For every context-free language $$L$$ there is a $$p$$ such that from any $$p$$ words $$s_1,\ldots,s_p\in L$$ we can select two, $$s$$ and $$s'$$, that can be written as $$s=uvwxy$$ and $$s'=u'v'w'x'y'$$ such that $$1\le |vx|\le p$$, $$1\le |v'x'|\le p$$ and every word $$\bar u\bar v_1\ldots \bar v_n \bar w\bar x_n \ldots \bar x_1 \bar y\in L$$, where $$\bar w$$ can be either $$w$$ or $$w'$$, and similarly, $$\bar v_i$$ can be either $$v$$ or $$v'$$ and $$\bar x_i$$ can be either $$x$$ or $$x'$$, but only such that $$\bar v_i=v$$ if and only if $$\bar x_i=x$$ (thus $$\bar v_i=v'$$ if and only if $$\bar x_i=x'$$), and $$\bar u=u$$ if and only if $$\bar y=y$$ and $$\bar u=u'$$ if and only if $$\bar y=y'$$. Moreover, if instead of $$p$$, we are given $$p\binom {n+4}4$$ words of length $$n$$, we can additionally suppose for the selected two words that $$|u|=|u'|$$, $$|v|=|v'|$$, $$|w|=|w'|$$, $$|x|=|x'|$$ and $$|y|=|y'|$$.

This statement can be proved essentially the same way as the pumping lemma, we just need to pick some $$s$$ and $$s'$$ for which the same rule is pumped. This can be done if $$p$$ is large enough since there are only a constant number of rules. In fact, we don't even need that the same rule is pumped, but only that the non-terminal symbol is the same in the pumped rule. For the moreover part, notice that for a word of length $$n$$ there are only $$\binom {n+4}4$$ options it can be broken into five subwords, thus the statement follows from the pigeonhole principle.

Next we give another way of generalizing the pumping lemma (and later we'll combine the two).

Nested Pumping Lemma. For every context-free language $$L$$ there is a $$p$$ such that for any $$k$$ any word $$s\in L$$ can be written as $$s=uv_1\ldots v_kwx_k\ldots x_1y$$ such that $$\forall i~1\le |v_ix_i|\le p$$ and for every sequence $$(i_j)_{j=1}^m$$ the word $$uv_{i_1}\ldots v_{i_m}wx_{i_m}\ldots x_{i_1}y\in L$$.

Note that the indices $$i_j$$ can be arbitrary from $$1$$ to $$k$$, the same index can occur multiple times. The proof of the Nested Pumping Lemma is essentially the same as the original pumping lemma's, we just need to use that we obtain the same non-terminal symbol from itself $$k$$ times - this is true if we do $$p(k-1)+1$$ steps (instead of the $$p$$ from the original pumping lemma). We can also strengthen Ogden's lemma in a similar way.

Nested Ogden's Lemma. For every context-free language $$L$$ there is a $$p$$ such that for any $$k$$ marking any at least $$p^k$$ positions in any word $$s\in L$$, it can be written as $$s=uv_1\ldots v_kwx_k\ldots x_1y$$ such that $$\forall i~1\le$$ '# of marks in $$v_ix_i$$'$$\le p^k$$ and for every sequence $$(i_j)_{j=1}^m$$ the word $$uv_{i_1}\ldots v_{i_m}wx_{i_m}\ldots x_{i_1}y\in L$$.

Unfortunately, in our application $$p^k$$ would be too large, so we need to weaken the conclusion to allow non-nested $$v_i$$-$$x_i$$ pairs. Luckily, using Dilworth, the structure stays simple.

Dilworth Ogden's Lemma. For every context-free language $$L$$ there is a $$p$$ such that for any $$k,\ell$$ marking any at least $$pk\ell$$ positions in any word $$s\in L$$, it can be written either as

case (i): $$s=uv_1\ldots v_kwx_k\ldots x_1y$$, or as

case (ii): $$s=uv_1w_1x_1\ldots v_\ell w_\ell x_\ell y$$,

such that $$\forall i~1\le$$ '# of marks in $$v_ix_i$$'$$\le pk\ell$$ and for every sequence $$(i_j)_{j=1}^m$$,

in case (i) the word $$uv_{i_1}\ldots v_{i_m}wx_{i_m}\ldots x_{i_1}y\in L$$, and

in case (ii) the word $$uv_1^{i_1}w_1x_1^{i_1}\ldots v_\ell^{i_\ell}w_\ell x_\ell^{i_\ell}y\in L$$.

Proof: Take the derivation tree generating $$s$$. Call a non-terminal recurring if it appears again under itself in the derivation tree. By expanding the rule set, we can suppose that all non-terminal symbols are recurring in the derivation tree. (This is to be understood that we might have eliminated their recurrence; this doesn't matter, the point is that they can be pumped.) There are at least $$pk\ell$$ leaves that correspond to a marked position. We look at the nodes where two marked letters split. There are at least $$\Omega(pk\ell)$$ such nodes. By the pigeonhole principle, at least $$\Omega(pk\ell)$$ correspond to the same non-terminal. Using Dilworth, $$\Omega(\sqrt{p}k)$$ of them are in a chain or $$\Omega(\sqrt{p}\ell)$$ are in an antichain, giving cases (i) and (ii), respectively, if $$p$$ is large enough.

Now we are ready to state a big combination lemma.

Super Lemma. For every context-free language $$L$$ there is a $$p$$ such that for any $$k,\ell$$ marking the same at least $$pk\ell$$ positions in $$N\ge \max(pn^{2k+2},pn^{3\ell+1})$$ words $$s_1,\ldots,s_N\in L$$, each of length $$n$$, there are two words, $$s$$ and $$s'$$, that can be written as $$s=uv_1\ldots v_kwx_k\ldots x_1y$$ and $$s'=u'v_1'\ldots v_k'w'x_k'\ldots x_1'y'$$ OR as $$s=uv_1w_1x_1\ldots v_\ell w_\ell x_\ell y$$ and $$s'=u'v_1'w_1'x_1'\ldots v_\ell'w_\ell'x_\ell'y'$$ such that the respective lengths of the subwords are all the same, i.e., $$|u|=|u'|$$, $$|v_i|=|v_i'|$$, etc., and $$\forall i$$ $$v_ix_i$$ contains a mark, and for every sequence $$(i_j)_{j=1}^m$$ the word $$\bar u\bar v_{i_1}\ldots \bar v_{i_m}\bar w\bar x_{i_m}\ldots \bar x_{i_1} \bar y\in L$$ OR $$\bar u\bar v_1^{i_1}\bar w_1\bar x_1^{i_1}\ldots \bar v_\ell^{i_\ell}\bar w_\ell \bar x_\ell^{i_\ell}\bar y \in L$$, respectively, where $$\bar z$$ stands for $$z$$ or $$z'$$, i.e., we can freely mix the intermediate subwords from $$s$$ and $$s'$$, but only such that $$\bar u=u$$ if and only if $$\bar y=y$$ etc.

Proof sketch of Super Lemma: Apply the Dilworth Ogden's Lemma for each $$s_i$$. There are $$\binom {n+2k+2}{2k+2}$$ and $$\binom {n+3\ell+1}{3\ell+1}$$ possible options, respectively, where the boundaries between the subwords of $$s_i$$ can be. There are a constant number of non-terminals in the language, thus by the pigeonhole principle, if $$p$$ is large enough, the same non-terminal is pumped in the $$k$$/$$\ell$$ rules for at least two words, $$s$$ and $$s'$$, that also have the same subword boundaries.

Unfortunately, the number $$N$$ that comes from this lemma is too large for our application. We can, however, decrease it by demanding fewer coincidences among the subwords. Now we state the lemma that we'll use.

Special Lemma. For every context-free language $$L$$ there is a $$p$$ such that for any $$k$$ marking the same at least $$pk$$ positions in $$N=pkn^2$$ words $$s_1,\ldots,s_N\in L$$, each of length $$n$$, there are two words, $$s$$ and $$s'$$, that can be written either as $$s=uv_1\ldots v_kwx_k\ldots x_1y$$ and $$s'=u'v_1'\ldots v_k'w'x_k'\ldots x_1'y'$$ such that either

case (i): $$\exists i such that $$|x_i|=|x_i'|=0$$, $$|uv_1\ldots v_{i-1}|=|u'v_1'\ldots v_{i-1}'|$$, and $$|v_i|=|v_i'|$$ (i.e., the two latter conditions mean that the position of $$v_i$$ is the same as the position of $$v_i'$$), OR

case (ii): $$\forall i $$|x_i|\ge 1$$, $$|x_i'|\ge 1$$, $$|uv_1\ldots v_{k-1}|=|u'v_1'\ldots v_{k-1}'|$$ and $$|v_kwx_k|=|v_k'w'x_k'|$$ (i.e., these two conditions mean that the position of $$v_kwx_k$$ is the same as the position of $$v_k'w'x_k'$$),

and (for both cases) $$\forall i$$ $$v_ix_i$$ contains a mark, and for every sequence $$(i_j)_{j=1}^m$$ the word $$\bar u\bar v_{i_1}\ldots \bar v_{i_m}\bar w\bar x_{i_m}\ldots \bar x_{i_1}\bar y\in L$$, where $$\bar z$$ stands for $$z$$ or $$z'$$, i.e., we can freely mix the intermediate subwords from $$s$$ and $$s'$$, but only such that $$\bar u=u$$ if and only if $$\bar y=y$$ etc., OR

case (iii): $$s$$ and $$s'$$ can be written as $$s=uv_1w_1x_1v_2w_2x_2y$$ and $$s'=u'v_1'w_1'x_1'v_2'w_2'x_2'y'$$ such that $$|u|=|u'|$$ and $$|v_1w_1x_1|=|v_1'w_1'x_1'|$$, and $$\forall i$$ $$v_ix_i$$ contains a mark, and $$uv_1^hw_1x_1^hv_2w_2x_2y\in L$$ and $$u'v_1^hw_1x_1^hv_2'w_2'x_2'y'\in L$$.

The proof only differs from the Super Lemma's that there are $$k\binom n2$$ possible options for a word in case (i), which leaves $$\binom n2$$ options for case (ii), while in case (iii) there are $$\binom n2$$ options.

Corollary. If for every $$p$$ there are $$t$$ and $$n$$ with $$n\ge p(t+1)+t$$ such that there are $$N=n^3$$ words of length $$n$$ in a context-free language $$L$$ whose first $$p(t+1)$$ letters are the same for each word, and their last $$t$$ letters are different for each pair or words (i.e., the words look like $$s_i=s_i^{beg}s_i^{mid}s_i^{end}$$ such that $$|s_i^{beg}|=p(t+1)$$, $$|s_i^{mid}|=n-p(t+1)-t$$, $$|s_i^{end}|=t$$, and $$\forall i\ne j~s_i^{beg}= s_j^{beg}$$ and $$s_i^{end}\ne s_j^{end}$$), then there is a $$B$$ such that there are infinitely many pairs of words $$a_h\ne b_h\in L$$ of equal length that differ only in their last $$B$$ letters.

Proof: Take $$k=t+1$$ and apply the Special Lemma for our $$N$$ words using $$N=n^3\ge p(t+1)n^2$$, marking the first $$p(t+1)$$ letters (that are the same in every word) to obtain $$s=uv_1\ldots v_{t+1}wx_{t+1}\ldots x_1y$$ and $$s'=u'v_1'\ldots v_{t+1}'w'x_{t+1}'\ldots x_1'y'$$ OR $$s=uv_1w_1x_1v_2w_2x_2y$$ and $$s'=u'v_1'w_1'x_1'v_2'w_2'x_2'y'$$.

If we are in case (i) of the Special Lemma, i.e., there is an $$i$$ such that $$|x_i|=|x_i'|=0$$, $$|uv_1\ldots v_{i-1}|=|u'v_1'\ldots v_{i-1}'|$$, and $$|v_i|=|v_i'|$$, then $$uv_1\ldots v_{i-1}=u'v_1'\ldots v_{i-1}'$$ and $$v_i=v_i'$$ also hold, as $$v_{i+1}wx_{i+1}$$ needs to contain a marked letter, thus the subwords preceding $$v_{i+1}$$ consist of only marked letters, and these are the same in $$s$$ and $$s'$$. We can take the words $$a_h=uv_1\ldots v_i^h\ldots v_twx_t\ldots x_1y$$ and $$b_h=uv_1\ldots v_i^hv_{i+1}'\ldots v_t'w'x_t'\ldots x_1'y'$$ to obtain the desired pairs; since these words end the same way as $$s$$ and $$s'$$, $$a_h\ne b_h$$ and they differ only in their last bounded many letters.

If we are in case (ii) of the Special Lemma, i.e., $$\forall i $$|x_i|\ge 1$$, $$|x_i'|\ge 1$$, $$|uv_1\ldots v_t|=|u'v_1'\ldots v_t'|$$ and $$|v_{t+1}wx_{t+1}|=|v_{t+1}'w'x_{t+1}'|$$, then $$uv_1\ldots v_t=u'v_1'\ldots v_t'$$ also holds, similarly as in the previous case. Now we can take $$a_h=uv_1\ldots v_tv_{t+1}^{h}wx_{t+1}^{h}x_t\ldots x_1y$$ and $$b_h=uv_1\ldots v_tv_{t+1}^{h}wx_{t+1}^{h}x_t'\ldots x_1'y'$$; since $$|x_t\ldots x_1y|=|x_t'\ldots x_1'y'|\ge t$$, these words certainly end differently and can differ only in their last bounded many letters. (Note that this is the only place where we really need that we can pump one word with a subword of the other one.)

If we are in case (iii) of the Special Lemma, i.e., $$s=uv_1w_1x_1v_2w_2x_2y$$ and $$s'=u'v_1'w_1'x_1'v_2'w_2'x_2'y'$$ such that $$|u|=|u'|$$ and $$|v_1w_1x_1|=|v_1'w_1'x_1'|$$, then $$u=u'$$ and $$v_1w_1x_1=v_1'w_1'x_1'$$ also hold, similarly as in the previous cases. Now we can take $$a_h=uv_1^hw_1x_1^hv_2w_2x_2y\in L$$ and $$b_h=uv_1^hw_1x_1^hv_2'w_2'x_2'y'\in L$$; since $$v_2$$ contains a marked letter, $$|v_2w_2x_2y|\ge t$$, thus these words certainly end differently and can differ only in their last bounded many letters.

This finishes the proof of the Corollary. Now let's see how to prove the original question from the Corollary.

Final proof. First we show that the condition of the Corollary is satisfied for every integer valued polynomial $$f$$. Set $$t=p-1$$ and $$n=C^p$$ for some large enough $$C=C(f)$$. The plan is to take some numbers $$x_1,\ldots,x_{2N}$$ (where $$N=2n^3$$) for which $$f(x_i)\ne f(x_j)$$, and then add some sufficiently large number $$z$$ to each of them to obtain the desired words $$s_i=f(x_i+z)$$. If the degree of $$f$$ is $$d$$, then at most $$d$$ numbers can take the same value, thus we can select $$x_1,\ldots,x_{2N}$$ from the first $$2dN$$ numbers, which means that they have $$\log(dn)$$ digits. In this case $$f(x_i)=O((dN)^d)$$, thus each $$f(x_i)$$ will have at most $$d\log N + O(1)=O(\log n)$$ digits. If we pick $$z$$ to be an $$n/d$$ digit number, then $$f(z)$$ will have $$n$$ digits, and for each $$f(x_i+z)$$ only the last $$O(\log n)$$ digits can differ. The $$f(x_i+z)$$ will all have $$n$$ or $$n+1$$ digits, thus at least half, i.e., $$N$$ of them have the same length; these will be the $$s_i$$.

From the conclusion of the Corollary we obtain infinitely many pairs of numbers $$a_h$$ and $$b_h$$, such that $$|a_h-b_h|\le 2^B$$, which is clearly impossible for polynomials.