# Thomae's function

Point plot on the interval (0,1). The topmost point in the middle shows f(1/2) = 1/2

Thomae's function, named after Carl Johannes Thomae, has many names: the popcorn function, the raindrop function, the countable cloud function, the modified Dirichlet function, the ruler function,[1] the Riemann function, or the Stars over Babylon (John Horton Conway's name).[2] This real-valued function of a real variable can be defined as:[3]

${\displaystyle f(x)={\begin{cases}{\frac {1}{q}}&{\text{if }}x={\tfrac {p}{q}}\quad (x{\text{ is rational), with }}p\in \mathbb {Z} {\text{ and }}q\in \mathbb {N} {\text{ coprime}}\\0&{\text{if }}x{\text{ is irrational.}}\end{cases}}}$

Since every rational number has a unique representation with coprime (=relatively prime) ${\displaystyle p\in \mathbb {Z} \;}$ and ${\displaystyle q\in \mathbb {N} }$, the function is well-defined. Note that ${\displaystyle q=+1}$ is the only number in ${\displaystyle \mathbb {N} }$ that is coprime to ${\displaystyle p=0.}$

It is a modification of the Dirichlet function, which is 1 at rational numbers and 0 elsewhere.

## Properties

• Thomae's function ${\displaystyle f}$ is bounded and maps all real numbers to the unit interval:${\displaystyle \;f:\mathbb {R} \;\rightarrow \;[0,\;1].}$
• ${\displaystyle f}$ is periodic with period ${\displaystyle 1:\;f(x+n)=f(x)\;}$ for all integer n and all real x.
Proof of periodicity

For all ${\displaystyle x\in \mathbb {R} \smallsetminus \mathbb {Q} \;:\quad f(x+n)=f(x)=0,}$ since ${\displaystyle (x+n)\in \mathbb {R} \smallsetminus \mathbb {Q} \;\Rightarrow x\in \mathbb {R} \smallsetminus \mathbb {Q} .}$

For all ${\displaystyle x\in \mathbb {Q} ,\;}$ there exist ${\displaystyle p\in \mathbb {Z} \;}$ and ${\displaystyle \;q\in \mathbb {N} ,}$ such that ${\displaystyle \;x=p/q,\;}$ and ${\displaystyle \gcd(p,\;q)=1.}$ According to Bézout's identity, for some ${\displaystyle a,\;b\in \mathbb {Z} }$ holds

${\displaystyle \gcd \left((p+nq),\;q\right)=ap+(an+b)q=\gcd(p,\;q)=1,}$

and thus ${\displaystyle f(x+n)=f({\tfrac {p+nq}{q}})={\tfrac {1}{q}}=f({\tfrac {p}{q}})=f(x).}$

• ${\displaystyle f}$ is discontinuous at all rational numbers, dense within the real numbers.
Proof of discontinuity at rational numbers

Assume an arbitrary rational ${\displaystyle x_{0}=p/q,\;}$ with ${\displaystyle \;p\in \mathbb {Z} ,\;q\in \mathbb {N} \;}$ and ${\displaystyle \;(p,\;q)\,}$ coprime.

This establishes ${\displaystyle \quad f(x_{0})=1/q.}$

Let ${\displaystyle \;\alpha \in \mathbb {R} \smallsetminus \mathbb {Q} \;}$ be any irrational number and define for all ${\displaystyle \;i\in \mathbb {N} ,\;}$ ${\displaystyle \quad x_{i}=x_{0}+{\frac {\alpha }{i}}.}$

These ${\displaystyle x_{i}}$ are all irrational, and so ${\displaystyle \quad f(x_{i})=0.}$

This implies ${\displaystyle \quad |x_{0}-x_{i}|={\frac {\alpha }{i}},\quad }$ and ${\displaystyle \quad |f(x_{0})-f(x_{i})|={\frac {1}{q}}.}$

Taking ${\displaystyle \;\varepsilon =1/q\;}$ and ${\displaystyle \;i=1+\left\lceil {\frac {\alpha }{\delta }}\right\rceil \;}$ selects an ${\displaystyle \;x_{i},\;}$ such that

${\displaystyle |f(x_{0})-f(x_{i})|=1/q\geq \varepsilon \quad }$ and

for all ${\displaystyle \delta >0\;:\;}$${\displaystyle |x_{0}-x_{i}|={\frac {\alpha }{1+\left\lceil {\frac {\alpha }{\delta }}\right\rceil }}<{\frac {\alpha }{\left\lceil {\frac {\alpha }{\delta }}\right\rceil }}\leq \delta ,}$

which is exactly the definition of discontinuity of ${\displaystyle f}$ at ${\displaystyle x_{0}}$.

This shows that ${\displaystyle f}$ is discontinuous on ${\displaystyle \;\mathbb {Q} .}$

• ${\displaystyle f}$ is continuous at all irrational numbers, also dense within the real numbers.
Proof of continuity at irrational arguments

Since ${\displaystyle f}$ is periodic with period ${\displaystyle 1,\;}$ and ${\displaystyle 0\in \mathbb {Q} ,\;}$ it suffices to check all irrational points in ${\displaystyle I=(0,\;1).\;}$ Assume now ${\displaystyle \varepsilon >0,\;i\in \mathbb {N} \;}$ and ${\displaystyle x_{0}\in I\smallsetminus \mathbb {Q} .\;}$ According to the Archimedean property of the reals, there exists ${\displaystyle r\in \mathbb {N} \;}$ with ${\displaystyle \;1/r<\varepsilon ,\;}$ and there exist ${\displaystyle \;k_{i}\in \mathbb {N} ,\;}$ such that

for ${\displaystyle \quad 1\leq i\leq r,\;i\in \mathbb {N} \;:}$ ${\displaystyle \quad 0<{\frac {k_{i}}{i}}

The minimal distance of ${\displaystyle x_{0}}$ to its i-th lower and upper bounds equals

${\displaystyle d_{i}:=\min\{\;|x_{0}-{\frac {k_{i}}{i}}|,\;|x_{0}-{\frac {(k_{i}+1)}{i}}|\;\}.}$

We define ${\displaystyle \delta }$ as the minimum of all the finitely many ${\displaystyle d_{i}.}$

${\displaystyle \delta :=\min _{1\leq i\leq r}\{d_{i}\},\;}$ so that

for all ${\displaystyle \quad 1\leq i\leq r\;:\quad |x_{0}-k_{i}/i|\geq \delta \quad }$ and ${\displaystyle \quad |x_{0}-(k_{i}+1)/i|\geq \delta .}$

This is to say, that all these rational numbers ${\displaystyle k_{i}/i,\;(k_{i}+1)/i,\;}$ are outside the ${\displaystyle \delta }$-neighborhood of ${\displaystyle x_{0}.}$

Now let ${\displaystyle x\in \mathbb {Q} \cap (x_{0}-\delta ,x_{0}+\delta )}$ with the unique representation ${\displaystyle x=p/q}$ where ${\displaystyle p,q\in \mathbb {N} }$ are coprime. Then, necessarily, ${\displaystyle q>r,\;}$ and therefore,

${\displaystyle f(x)=1/q<1/r<\varepsilon .}$

Likewise, for all irrational ${\displaystyle x\in I,\;f(x)=0=f(x_{0});\;}$ and thus, if ${\displaystyle \epsilon >0}$ then any choice of (sufficiently small) ${\displaystyle \delta >0}$ gives

${\displaystyle |x-x_{0}|<\delta \implies |f(x_{0})-f(x)|=f(x)<\varepsilon .}$

Therefore, ${\displaystyle f}$ is continuous on ${\displaystyle \mathbb {R} \smallsetminus \mathbb {Q} .\quad }$

• ${\displaystyle f}$ is nowhere differentiable.
Proof of being nowhere differentiable
• For rational numbers, this follows from non-continuity.
• For irrational numbers:
All sequences of irrational numbers ${\displaystyle (a_{i}\neq x_{0})_{i=1}^{\infty }\,,\;}$ converging to the irrational point ${\displaystyle x_{0},\;}$ imply a constant sequence ${\displaystyle (f(a_{i})=0)_{i=1}^{\infty },\;}$ identical to ${\displaystyle 0,\;}$ and so ${\displaystyle \lim _{i\to \infty }\left|{\frac {f(a_{i})-f(x_{0})}{a_{i}-x_{0}}}\right|=0.}$
According to Hurwitz's theorem, there also exists a sequence of rational numbers ${\displaystyle (b_{i}=k_{i}/i)_{i=1}^{\infty },\;}$ converging to ${\displaystyle x_{0},\;}$ with ${\displaystyle (k_{i}\in \mathbb {Z} ,\;i\in \mathbb {N} )}$ coprime and ${\displaystyle |k_{i}/i-x_{0}|<{\frac {1}{{\sqrt {5}}\cdot i^{2}}}.\;}$
Thus for all ${\displaystyle i\,:\;\left|{\frac {f(b_{i})-f(x_{0})}{b_{i}-x_{0}}}\right|>{\frac {1/i-0}{1/({\sqrt {5}}\cdot i^{2})}}={\sqrt {5}}\cdot i\neq 0\;}$ and so ${\displaystyle f}$ is not differentiable at all irrational ${\displaystyle x_{0}.}$
See the proofs for continuity and discontinuity above for the construction of appropriate neighbourhoods, where ${\displaystyle f}$ has maxima.
• ${\displaystyle f}$ is Riemann integrable on any interval and the integral evaluates to ${\displaystyle 0}$ over any set.
The Lebesgue criterion for integrability states that a bounded function is Riemann integrable if and only if the set of all discontinuities has measure zero.[4] Every countable subset of the real numbers - such as the rational numbers - has measure zero, so the above discussion shows that Thomae's function is Riemann integrable on any interval. The function's integral is equal to ${\displaystyle 0}$ over any set because the function is equal to zero almost everywhere.

## Follow-up

A natural follow-up question one might ask is if there is a function which is continuous on the rational numbers and discontinuous on the irrational numbers. This turns out to be impossible; the set of discontinuities of any function must be an Fσ set. If such a function existed, then the irrationals would be an Fσ set. The irrationals would then be the countable union of closed sets ${\displaystyle \textstyle \bigcup _{i=0}^{\infty }C_{i}}$, but since the irrationals do not contain an interval, nor can any of the ${\displaystyle C_{i}}$. Therefore, each of the ${\displaystyle C_{i}}$ would be nowhere dense, and the irrationals would be a meager set. It would follow that the real numbers, being a union of the irrationals and the rationals (which is evidently meager), would also be a meager set. This would contradict the Baire category theorem: because the reals form a complete metric space, they form a Baire space, which cannot be meager in itself.

A variant of Thomae's function can be used to show that any Fσ subset of the real numbers can be the set of discontinuities of a function. If ${\displaystyle A=\textstyle \bigcup _{n=1}^{\infty }F_{n}}$ is a countable union of closed sets ${\displaystyle F_{n}}$, define

${\displaystyle f_{A}(x)={\begin{cases}{\frac {1}{n}}&{\text{if }}x{\text{ is rational and }}n{\text{ is minimal so that }}x\in F_{n}\\-{\frac {1}{n}}&{\text{if }}x{\text{ is irrational and }}n{\text{ is minimal so that }}x\in F_{n}\\0&{\text{if }}x\notin A\end{cases}}}$

Then a similar argument as for Thomae's function shows that ${\displaystyle f_{A}}$ has A as its set of discontinuities.

For a general construction on arbitrary metric space, see this article Kim, Sung Soo. "A Characterization of the Set of Points of Continuity of a Real Function." American Mathematical Monthly 106.3 (1999): 258-259.

## Related probability distributions

Empirical probability distributions related to Thomae's function appear in DNA sequencing.[5] The human genome is diploid, having two strands per chromosome. When sequenced, small pieces ("reads") are generated: for each spot on the genome, an integer number of reads overlap with it. Their ratio is a rational number, and typically distributed similarly to Thomae's function.

If pairs of positive integers ${\displaystyle m,n}$ are sampled from a distribution ${\displaystyle f(n,m)}$ and used to generate ratios ${\displaystyle q=n/(n+m)}$, this gives rise to a distribution ${\displaystyle g(q)}$ on the rational numbers. If the integers are independent the distribution can be viewed as a convolution over the rational numbers, ${\displaystyle g(a/(a+b))=\sum _{t=1}^{\infty }f(ta)f(tb)}$. Closed form solutions exist for power-law distributions with a cut-off. If ${\displaystyle f(k)=k^{-\alpha }e^{-\beta k}/\mathrm {Li} _{\alpha }(e^{-\beta })}$ (where ${\displaystyle \mathrm {Li} _{\alpha }}$ is the polylogarithm function) then ${\displaystyle g(a/(a+b))=(ab)^{-\alpha }\mathrm {Li} _{2\alpha }(e^{-(a+b)\beta })/\mathrm {Li} _{\alpha }^{2}(e^{-\beta })}$. In the case of uniform distributions on the set ${\displaystyle \{1,2,\ldots ,L\}}$ ${\displaystyle g(a/(a+b))=(1/L^{2})\lfloor L/\max(a,b)\rfloor }$, which is very similar to Thomae's function. Both their graphs have fractal dimension 3/2.[5]

## The ruler function

For integers, the exponent of the highest power of 2 dividing ${\displaystyle n}$ gives 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, ... (sequence A007814 in the OEIS). If 1 is added, or if the 0s are removed, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, ... (sequence A001511 in the OEIS). The values resemble tick-marks on a 1/16th graduated ruler, hence the name. These values correspond to the restriction of the Thomae function to those rational numbers whose denominators are powers of 2.