自动化:埃拉托斯特尼筛法的静态数组

自动化:埃拉托斯特尼筛法的静态数组

我想要生成一个维度为 10*10 的从 1 到 100 的简单数字数组,其中 1 不会显示。

tikz 中的埃拉托斯特尼筛法有漂亮的动画,但我需要的只是一个静态数组,供学生用于进行埃拉托斯特尼筛选法的实验。

答案1

以下是tikz 中的埃拉托斯特尼筛法仅需要生成初始网格。

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{xstring}%         String comparison
\usepackage{tikz}%  
\usetikzlibrary{calc}

\def\NumOfColumns{10}%   
\def\NumOfRows{10}%   

%%% Extracted from % https://tex.stackexchange.com/questions/44673/sieve-of-eratosthenes-in-tikz

%%% Step 1: Create a list of integers 2...n
%%%
\newcommand*{\DrawGridWithNumbers}{%
    \begin{scope}[draw=gray, thick]% Add numbers to each node
        \draw  (0,-1) -- ($(0,-\NumOfRows-1)$);
        \foreach \col in {1,...,\NumOfColumns} {%
            \draw  (\col,-1) -- ($(\col,-\NumOfRows-1)$);

            \draw  (0,-1) -- (\NumOfColumns,-1);
            \foreach \row in {1,...,\NumOfRows}{%
                \pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
                \IfEq{\value}{1}{
                    %% Suppress number 1 from being printed since first  
                    %% step of Sieve of Eratosthenes algorithm is to 
                    %% create a list of integers 2...n
                }{
                    \node at ($(\col,-\row)-(0.5,0.5)$) {\textbf{\value}};
                }
                \draw (0,-\row-1) -- (\NumOfColumns,-\row-1);
            }
        }
    \end{scope}
}


\begin{document}
\begin{tikzpicture}
  \DrawGridWithNumbers;
\end{tikzpicture}
\end{document}

答案2

一个简单的解决方案就是\numexpr将一个添加到变量\x并将其打印在每个单元格中:

代码结果

\documentclass{article}

\usepackage{array}
\newcommand{\tabstrut}{\vrule height 1.25em depth 0.5em width 0pt}

\begin{document}

\xdef\x{0}
\begin{tabular}{|*{10}{>{\tabstrut\xdef\x{\the\numexpr\x+1\relax}\ifnum\x=1 \else\x\fi}c|}}
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
&&&&&&&&&\\
\hline
\end{tabular}

\end{document}

答案3

在此处输入图片描述

\documentclass{article}

\begin{document}

\fbox{\begin{picture}(230,220)
\global\count1=0
\multiput(10,180)(0,-20){10}{%
\multiput(10,10)(20,0){10}{%
  \global\advance\count1 1
   \framebox(15,15){\ifnum\count1>1 \the\count1 \fi}
}}
\end{picture}}

\end{document}

答案4

“Variatio delectat”,一种使用简单表格环境的解决方案:

\documentclass{article}
\usepackage{array}
\newsavebox\CellBox
\begin{document}
\begingroup
  \makeatletter
  \setlength{\tabcolsep}{.5\tabcolsep}%
  \sbox\CellBox{100}%
  \sbox\strutbox{%
    \vrule
      height.5\dimexpr\wd\CellBox+2\tabcolsep+\ht\CellBox-\dp\CellBox\relax
      depth.5\dimexpr\wd\CellBox+2\tabcolsep-\ht\CellBox+\dp\CellBox\relax
      width0pt%
  }%
  \begin{tabular}{|*{10}{>{\centering}p{\wd\CellBox}|}}
    \hline &%
    \toks@{}%
    \count@=1%
    \@whilenum\count@<99 \do{%
      \advance\count@\@ne
      \toks@\expandafter{%
        \the\expandafter\toks@
        \the\expandafter\expandafter\expandafter\count@
        \ifnum1\expandafter\@cdr\the\count@\@nil=10 %
          \expandafter\tabularnewline
          \expandafter\hline
        \else
          \expandafter&%
        \fi
      }%
    }%
    \the\toks@
    100\tabularnewline\hline
  \end{tabular}%
\endgroup
\end{document}

结果

另外,使用矩形形式来节省垂直空间也不难:

\documentclass{article}
\usepackage{array}
\newsavebox\CellBox
\begin{document}
\begingroup
  \makeatletter
  \setlength{\tabcolsep}{.5\tabcolsep}%
  \sbox\CellBox{100}%
  \sbox\strutbox{%
    \vrule
      height\dimexpr\ht\CellBox+\tabcolsep\relax
      depth\dimexpr\dp\CellBox+\tabcolsep\relax
      width0pt%
  }%
  \begin{tabular}{|*{10}{>{\centering}p{\wd\CellBox}|}}
    \hline &%
    \toks@{}%
    \count@=1%
    \@whilenum\count@<99 \do{%
      \advance\count@\@ne
      \toks@\expandafter{%
        \the\expandafter\toks@
        \the\expandafter\expandafter\expandafter\count@
        \ifnum1\expandafter\@cdr\the\count@\@nil=10 %
          \expandafter\tabularnewline
          \expandafter\hline
        \else
          \expandafter&%
        \fi
      }%
    }%
    \the\toks@
    100\tabularnewline\hline
  \end{tabular}%
\endgroup
\end{document}

结果

相关内容