TITLE: is f a polynomial provided that it is "partially" smooth?
QUESTION [30 upvotes]: Let $f$ be a $C^\infty$ function on $(c,d)$ ,and
let $O=\cup_{n\in \mathbb{Z}^+} (a_n,b_n)$ where $(a_n,b_n)$  are disjoint open interval in $(c,d)$ and $O$ is dense in $(c,d)$.
Suppose for each $n\in \mathbb{Z}^+$ ,$f$ coincides with a polynomial on $(a_n,b_n)$.
Is it necessary that $f$ coincide with a polynomial on $(c,d)$?

REPLY [22 votes]: The existence of such functions being established by now, there remains a curiosity
for the concrete situations that may generate them. Here is one, which is also
interesting as an example of a $C^\infty$, non-analytic solution, of a simple  first order linear delay differential equation.
Let's consider the  problem
$$f'(x)=\lambda f(3x),\qquad x < \frac{1}{2}  $$
where we seek for a function $f\in C^1(\mathbb{R})$ with support in
$I:=[0,1]$, and with the condition $f(x)=f(1-x)$.
I will show that it has a simple eigenvalue $\lambda= \frac{9}{2}$,
corresponding to an
eigenfunction with $f(\frac{1}{2})=1$ (see the details below). Then, it is
very easy
to check that $f$ is locally polynomial on the complement of the Cantor
set; and it
is, of course, $C^\infty$.
Indeed, since $f (3x)=0$ for $  x > \frac{1}{3}$, from the equation we
have that
$f'(x)$ vanishes identically on the interval $(\frac{1}{3},\frac{2}{3})$,
so that
$f$ is the constant $1$ therein. By induction, it is easy to see that $f$ is
polynomial of degree $k-1$ on each component of the complement of the
closed set
$F_k$ in $I$, inductively defined by $F_0:=I$ and $F_{k+1}:= \frac{1}{3}F_k
\cup\left(\frac{1}{3}F_k + \frac{2}{3}\right)$. Note that  $\{F_k\} _
{k\in\mathbb{N}}$ is the well-known nested sequence of closed sets whose
intersection is the Cantor set.
$$*$$
Construction. Consider the linear   operator $T$ on
$L^\infty(0,1)$ (thought as subspace of $L^\infty(\mathbb{R})$ via extension by zero),
defined by
$$Tu(x)= \cases{  \int_0^{3x}u(t)\, dt\qquad \qquad 0 < x < \frac{1}{3}\\\ 0
\qquad \qquad \qquad \qquad \frac{1}{3} < x < \frac{2}{3}\\\
\int_0^{3(1-x)}u(t)\, dt  \qquad\frac{2}{3}< x < 1.} $$
Since $T$ and $T ^ 2$ are integral operators with non-negative kernels,
their norms are attained on the constant function $1$ on $[0,1]$. We have, by simple
computations $T1(x)=3\min(x,1-x)\chi _ { F _ 1 }(x) $,
$\|T\| = \|T1\| _ \infty=1$ and $\|T^2\|=\|T(T1)\|_\infty=\frac{1}{3}$.
Therefore $\rho(T)\le\frac{1}{\sqrt 3} < \frac{2}{3}$ and $I-\frac{3}{2}T$ is
invertible. The function
$$f:= \left(I-\frac{3}{2}T\right) ^ {-1}\chi _
{[\frac{1}{3},\frac{2}{3}]} = \sum _ {k=0} ^ \infty  \left( \frac{3}{2}\right) ^ k T
^ k \chi _
 { [\frac{1}{3}, \frac{2}{3}] } $$
verifies
$$f(x)=\frac{3}{2} \int_0^{3x}f(t)\; dt\qquad \mathrm{for\; all}\quad 0 < x <
\frac{1}{3}\, ,$$
$$f(x)=1\qquad \mathrm{for\, all}\quad \frac{1}{3} < x < \frac{2}{3}\, ,$$
and $$f(x)=f(1-x)\qquad \mathrm{for\, all}\quad x\in\mathbb{R} \, .$$
The only points in $\mathbb{R}$ where the continuity of $f$ is not immediate and has
to be checked, are  $\frac{1}{3}$ and  $\frac{2}{3}$. We have
$$\int_ 0 ^ 1 f(x)dx=\int _ 0 ^ \frac{1}{3} f(x)dx+\int _ \frac{1}{3} ^ \frac{2}{3}
f(x)dx+\int _ \frac{2}{3} ^ 1 f(x)dx= 2\int _ 0 ^ \frac{1}{3} f(x)dx+ \frac{1}{3}\,
,$$
and
$$ 2  \int  _ 0  ^ \frac{1}{3}    f(x)dx 
=  3\int _ 0 ^ \frac{1}{3}  \int _ 0 ^ {3x} f(t) dt dx= \int _ 0 ^ 1 \int _ 0 ^ x
f(t) dt dx=$$
$$= \int _ 0 ^ 1 (1-t) f(t) dt = \int _ 0 ^ 1 tf(1-t)dt=   \int _ 0 ^ 1 tf(t)dt = $$
$$ =\frac{1}{2}\left(\int _ 0 ^ 1 (1-t)f(t)+ tf(t)dt\right) =\frac{1}{2} \int _ 0 ^
1  f(t)dt\, . $$
So
$$  \int _ 0 ^ 1f(t)dt = \frac{2}{3}\, ,$$
which implies the continuity of $f$ at $  \frac{1}{3}$ (and by symmetry at $
\frac{2}{3}$ ), since
$$f(\frac{1}{3}-)=\frac{3}{2}\int_ 0 ^ 1 f(x)dx =1= f(\frac{1}{3}+) \, .   $$
As a consequence, $f$ actually satisfies
$$f(x)=\frac{3}{2} \int_0^{3x}f(t)\, dt  $$
for all $  x < \frac{2}{3}\, ,$
and we conclude that $f$ is in $ C ^ 1(\mathbb{R})$,   and has the stated properties.

Here is  a Pudding Function $f$, and its integral function, $F(x):=\int_0^x f(t)dt$. Note how the latter coincides with the first third of the former, up to rescaling. Simple considerations based on odd and even symmetry show that $F(1)=\frac{2}{3}$.
Edit 2021 I learned from this answer by Gerald Edgar that  a function quite similar (but not easily related) to this is known since 1966 as the Fabius function. It verifies $F'(x)=2F(2x)$ instead of $f'(x)=\frac{9}{2}f(3x)$.