Example: Ulam spiral

Published 2007-03-28 | Author: Alain Matthes

The Ulam spiral reveals some interesting prime number patters. The code is an excellent example of how TeX' control logic can be used to create complex drawings. Experiment with the code to reveal other prime number patterns.

Author:Alain Matthes
Source:Altermundus

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Ulam spiral

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% Ulam spiral
% Author: Alain Matthes (http://altermundus.fr/)

\documentclass[]{article}
\usepackage{ifthen,fullpage}
\usepackage{tikz}
\newcounter{cmpt}
\newcounter{next}
\newcounter{end}
\makeatletter
\newcounter{prim@a}
\newcounter{prim@n}
\newcounter{prim@d}
\newcounter{prim@b}
\newboolean{reponse}

\newcommand*\primality[1]
{\setboolean{reponse}{true}
 \setcounter{prim@n}{#1}
 \ifthenelse{\value{prim@n}=0}% n = 0
   {\setboolean{reponse}{false}}
   {\ifthenelse{\value{prim@n}=1}% n =1
     {\setboolean{reponse}{false}}%
     {\ifthenelse{\value{prim@n}=2}{}% n = 2
       {\ifthenelse{\value{prim@n}=3}{}% n = 3
         {\ifthenelse{\isodd{\value{prim@n}}}% n > 3 parité ?
            {\setcounter{prim@a}{#1}
             \setcounter{prim@d}{3} % n impair on continue
            \whiledo{\value{prim@a}>\value{prim@d}}% deb whiledo
              {\setcounter{prim@a}{\value{prim@n}/\value{prim@d}}
               \setcounter{prim@b}{\value{prim@a}*\value{prim@d}}
               \ifthenelse{\value{prim@b}=\value{prim@n}}%
                 {\setboolean{reponse}{false}
                  \setcounter{prim@d}{\value{prim@a}}}%
                 {\addtocounter{prim@d}{2}%
                 }%
              }% end whiledo
           }
           {\setboolean{reponse}{false}}% n est pair
         }%
       }%
     }%
   }
}%end macro
\makeatother

\newcommand*\ballcolor{%
    \stepcounter{cmpt}
    \primality{\value{cmpt}}
    \ifthenelse{\boolean{reponse}}%
        { \colorlet{colornb}{blue!50}}%
        { \colorlet{colornb}{red!30}}%
}

\newcommand*{\ulam}[2]{%
    \setcounter{cmpt}{#1}
    \addtocounter{cmpt}{-1}
    \setcounter{next}{0}
    \setcounter{end}{#2}

    \ballcolor
    \node(place) at (0,0)  {$\mathbf\thecmpt$};

    \whiledo{\value{cmpt}<\value{end}}%
    {
            \foreach \i in {0,...,\thenext}
            {\ballcolor
            \node[above of=place] (place) {$\mathbf\thecmpt$};
        }

        \foreach \i in {0,...,\thenext}
        {
            \ballcolor
            \node[right of=place] (place) {$\mathbf\thecmpt$};
        }
        \stepcounter{next}

        \foreach \i in {0,...,\thenext}
        {
            \ballcolor
            \node[below of=place] (place) {$\mathbf\thecmpt$};
        }

        \foreach \i in {0,...,\thenext}
        {
            \ballcolor
            \node[left of=place] (place) {$\mathbf\thecmpt$};
        }
        \stepcounter{next}
    }
    \addtocounter{next}{-1}
    \foreach \i in {0,...,\thenext}
    {
        \ballcolor
        \node[above of=place] (place) {$\mathbf\thecmpt$};}
}
\begin{document}

\begin{center}
  \begin{tikzpicture}
  \tikzstyle{every node}=[draw,%
                          rectangle,%
                          shade,%
                          minimum size  = 1.6cm,%
                          ball color    = colornb,%
                          node distance = 1.6cm]

  \ulam{41}{113}

  \end{tikzpicture}
\end{center}
\end{document}

Comments

  • #1 kiran, June 3, 2009 at 7:05 a.m.

    This looks useful. But very complicated to uderstand the tex. Anyways.. I like these TIKZ features.

  • #2 Søren Nielsen, May 15, 2010 at 5:54 p.m.

    If you send me an email at srentospace@hotmail.com I can return my e-book about the Ulam spiral.

    Best regards Søren Nielsen

Adding comments is currently not enabled.