埃拉托斯特尼筛法

埃拉托斯特尼筛法

我看了@AlainMatthes 的回答tikz 中的埃拉托斯特尼筛法我在其中添加了建议的额外代码:

代码

\documentclass{article}
\usepackage{tikz}

\makeatletter
\tkzutil@ifundefined{pgfmath@function@isprime}{%
\newif\ifpgf@unknown
\newif\ifpgf@isaprime    
\pgfmathdeclarefunction{isprime}{1}{%
    \begingroup%
\pgf@unknowntrue
\c@pgfmath@counta#1\relax% 
 \ifcase#1\relax 
 % |#1| = 0 
 \pgf@isaprimefalse\pgf@unknownfalse
 \or
 % |#1| = 1
  \pgf@isaprimefalse\pgf@unknownfalse
  \or
% |#1| = 2
  \pgf@isaprimetrue\pgf@unknownfalse
 \or
% |#1| = 3
  \pgf@isaprimetrue\pgf@unknownfalse
   \else
 % |#1| > 3
   \ifodd#1\relax 
   \else
    \pgf@isaprimefalse\pgf@unknownfalse
   \fi
 \fi  
\ifpgf@unknown
\c@pgfmath@countd=\thr@@ \pgf@isaprimetrue
 \loop
 \c@pgfmath@countb=\c@pgfmath@counta  
 \divide\c@pgfmath@countb by\c@pgfmath@countd
  \ifnum\c@pgfmath@countb>\c@pgfmath@countd \pgf@unknowntrue
  \else\pgf@unknownfalse\fi
    \multiply\c@pgfmath@countb by\c@pgfmath@countd
  \ifnum\c@pgfmath@countb=\c@pgfmath@counta  
   \global\pgf@isaprimefalse\pgf@unknownfalse\fi
  \ifpgf@unknown\advance\c@pgfmath@countd by 2\relax% 
 \repeat
\fi
\ifpgf@isaprime
  \def\pgfmathresult{1}%
\else
  \def\pgfmathresult{0}%
\fi
 \pgfmath@smuggleone\pgfmathresult%
\endgroup
}}{}
\makeatother

\begin{document}

\begin{tikzpicture}[y=-1cm]
  \foreach \x in {0,...,9}
    \foreach \y in {1,...,10}
    { \draw (\x,\y) +(-.5cm,-.5cm) rectangle ++(.5cm,.5cm);
      \pgfmathtruncatemacro{\nb}{\x*10+\y}
       \ifnum\nb=1 \def\pgfmathresult{1}
       \else
      \node[minimum size= 1cm](last) at (\y-1,\x+1) {\nb};
      \pgfmathisprime{\nb}
      \fi
     \ifnum\pgfmathresult=0
      \draw[red](last.north west)--(last.south east)
                (last.north east)--(last.south west);
      \fi}
\end{tikzpicture}

\end{document}

当我编译时,出现以下错误:

! Undefined control sequence.
l.5 \tkzutil@ifundefined
                        {pgfmath@function@isprime}{%

我究竟做错了什么?

相关内容