Setup Menus in Admin Panel

  • LOGIN
  • No hay productos en el carrito.

Espiral de Ulam usando TikZ

Te comparto el código fuente de una figura que elaboré usando TikZ de LaTeX2e para generar la espiral de Ulam.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{ifthen}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0.125pt}
%
% File name: Ulam-spiral.tex
% Description: 
% The Ulam spiral is shown.
% 
% Date of creation: October, 21st, 2023.
% Date of last modification: October, 21st, 2023.
% Author: Efraín Soto Apolinar.
% https://www.aprendematematicas.org.mx/author/efrain-soto-apolinar/instructing-courses/
%
% Terms of use:
% https://creativecommons.org/licenses/by-nc-sa/4.0/
% Your commitment to the terms of use is greatly appreciated.
%
\begin{document}
	%
	\begin{tikzpicture}
		\pgfmathsetmacro{\l}{50} % length of the side of the square
		\pgfmathsetmacro{\n}{\l * \l}	% number of terms
		\pgfmathsetmacro{\m}{int(\n - 1)}
		%
		\draw[fill=red!15] (0,0) rectangle (1,1) node[gray,shift=({(-0.5,-0.5)})] {$1$};
		%
		\foreach \i [remember=\x as \xlast (initially 0), remember=\y as \ylast (initially 0),
			remember=\dx as \dxlast (initially 0), remember=\dy as \dylast (initially -1)] in {1,2,...,\m}{
			\tikzmath{\x = \xlast; \y = \ylast; \dx = \dxlast; \dy = \dylast;}
			\pgfmathsetmacro{\j}{int(\i + 1)}
			\pgfmathsetmacro{\d}{or(or(\x == \y, and(\x < 0, \x == -\y)), or(and(\x < 0, \x == -\y), and(\x > 0, \x == 1 - \y)))}
			% To compute location
			\ifthenelse{\d=1}{
				\pgfmathsetmacro{\t}{\dx}
				\pgfmathsetmacro{\dx}{-\dy}
				\pgfmathsetmacro{\dy}{\t}
			}{}
			% Location of the current term
			\pgfmathsetmacro{\x}{\xlast + \dx}
			\pgfmathsetmacro{\y}{\ylast + \dy}
			\pgfmathsetmacro{\primality}{isprime(\j)}
			%
			\ifthenelse{\primality=1}{
				\draw[fill=cyan!25,shift={(\x,\y)}] (0,0) rectangle (1,1) node[shift=({(-0.5,-0.5)})] {$\j$};
			}{
				\draw[fill=gray!15,shift={(\x,\y)}] (0,0) rectangle (1,1) node[gray,shift=({(-0.5,-0.5)})] {$\j$};
			}
		}
		%
	\end{tikzpicture}
	%
\end{document}

Si aún no conoces LaTeX2e, te invito a aprenderlo con nuestro curso en línea gratuito, titulado LaTeX2e para principiantes.

El resultado final se muestra a continuación.

Espiral de Ulam
octubre 22, 2023

0 responses on "Espiral de Ulam usando TikZ"

    Leave a Message

    Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

    Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

    X