2

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

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.

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

1 Answer 1

6

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}

enter image description here

4
  • Just a curiosity. What is the scope of this graphic? +1
    – Sebastiano
    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?
    – Sebastiano
    Sep 16, 2021 at 20:44
  • 1
    @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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .