# Graph of Thomae function by an amateur

I am trying to plot the Thomae function, which is defined in the following way:

Now, I went through this answer, but unfortunately, could not grasp much. My idea is simple. Run two nested loops for the rationals and plot the points. What I have done is the following:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=5]
\node [fill, circle, inner sep=0.5pt] at (0,0) {};
\node [fill, circle, inner sep=0.5pt] at (1,1) {};
\foreach [evaluate=\n as \den using \n-1] \n in {2,...,85}
\foreach \m in {1,...,\den}
\node [fill, circle, inner sep=0.5pt] at ({\m/\n},{1/\n}) {};
\end{tikzpicture}
\end{document}


I am getting stuck at the gcd part. I do not know the proper usages and syntaxes of pgfmathsetmacro and the conditional ifthenelse statements. Please help.

• What is wrong with the solution presented in the answer you linked to? Sep 13, 2021 at 20:08

I think that this is what you are looking for. I wrote my code with \pgfmathtruncatemacro (better than \pgfmathsetmacro as we need integer numbers), and a conditional statement with \ifnum.

\documentclass[tikz,border=2mm]{standalone}

\def\maxden{50} % maximum denominator

\begin{document}
\begin{tikzpicture}[scale=5]
\foreach\d in {2,...,\maxden}        % denominators from 2 to maximum
{
\pgfmathtruncatemacro\maxnum{\d-1} % maximum numerator
\foreach\n in {1,...,\maxnum}      % numerators from 2 to maximum
{
\pgfmathtruncatemacro\gcd{gcd(\n,\d)}
\ifnum\gcd = 1 % then the fraction is irreducible, so we draw a point
\fill (\n/\d,1/\d) circle (0.1pt);
\fi
}
}
\end{tikzpicture}
\end{document}


• Just a curiosity. What is the scope of this graphic? +1 Sep 15, 2021 at 21:35
• @Sebastiano, what do you mean? The domain? Sep 16, 2021 at 6:00
• I do mean what is the usefulness of these types of diagrams? Sep 16, 2021 at 20:44
• @Sebastiano, I'm not sure. I didn't know Thomae's function before, but I think it's pretty hard to visualize without the help of some kind of plot. Sep 17, 2021 at 6:08