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

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/)


 \ifthenelse{\value{prim@n}=0}% n = 0
   {\ifthenelse{\value{prim@n}=1}% n =1
     {\ifthenelse{\value{prim@n}=2}{}% n = 2
       {\ifthenelse{\value{prim@n}=3}{}% n = 3
         {\ifthenelse{\isodd{\value{prim@n}}}% n > 3 parité ?
             \setcounter{prim@d}{3} % n impair on continue
            \whiledo{\value{prim@a}>\value{prim@d}}% deb whiledo
              }% end whiledo
           {\setboolean{reponse}{false}}% n est pair
}%end macro

        { \colorlet{colornb}{blue!50}}%
        { \colorlet{colornb}{red!30}}%


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

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

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

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

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

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




  • #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.