5

I would like to produce one simple array of numbers from 1 to 100 of dimension 10*10, where 1 will not be displayed.

In Sieve of Eratosthenes in tikz there are pretty animations, but what I need is only one static array that will be used by students for experimenting with the sieve of Eratosthenes.

projetmbc
  • 13,315

5 Answers5

9

A straightforward solution just using \numexpr to add one to a variable \x and print it in each cell:

result of the code

\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}
Moriambar
  • 11,466
6

Here is the relevant portion from the Sieve of Eratosthenes in tikz needed to produce just the initial the grid.

enter image description here

Code:

\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}

Peter Grill
  • 223,288
6

enter image description here

\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}
David Carlisle
  • 757,742
6

I think that LuaTeX is ideal for such tasks. Below is a ConTeXt solution which should be easy to translate to LaTeX (it just sets the elements in a table).

\startsetups table:square
  \setupTABLE[width=2em, height=2em, align={middle,lohi}]
\stopsetups

\starttext
\startluacode
  context.bTABLE({"setups=table:square"})
  local flag; flag = false;
  for i=0,9 do
    context.bTR()
    for j=1,10 do 
        context.bTD()
        if flag then 
            context.math(i*10 + j)
        else
           flag = true
        end
        context.eTD()
    end
    context.eTR()
  end
  context.eTABLE()
\stopluacode

\stoptext

enter image description here

The biggest advantage is that the code is easy to read and understand. Using a "normal" programming language also makes it easy to add more bells and whistles. For example, here is a version that highlights the primes. I use a naive algorithm to check for a prime, and simply set the background color if the number is a prime.

\startsetups table:square
  \setupTABLE[width=2em, height=2em, align={middle,lohi}, background=color]
\stopsetups

\startluacode
    -- This is a naive implementation just for illustration
    thirddata = thirddata or {}
    function thirddata.isPrime(num) 
        if (num == 2) then 
            return true 
        end

        if (num <= 1) or (num % 2 == 0) then 
            return false 
        end

        for i = 3, num^(1/2), 2 do
           if  num % i  == 0 then 
              return false 
           end
        end

        return true
    end

\stopluacode

\starttext
\startTEXpage[offset=3mm]
\startluacode
  context.bTABLE({"setups=table:square"})
  local flag; flag = false;

  local function setBackground(boolean) 
      if boolean then
          return "backgroundcolor=lightred"
      else
          return ""
      end
  end

  for i=0,9 do
    context.bTR()
    for j=1,10 do 
        context.bTD({setBackground(thirddata.isPrime(i*10 + j))})
        if flag then 
            context.math(i*10 + j)
        else
           flag = true
        end
        context.eTD()
    end
    context.eTR()
  end
  context.eTABLE()
\stopluacode
\stopTEXpage

\stoptext

enter image description here

Again, the main advantage of LuaTeX is the readability of the code.

Aditya
  • 62,301
5

"Variatio delectat", a solution using a simple tabular environment:

\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}

Result

Also it is not difficult to save vertical space, using a rectangular form:

\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}

Result

Heiko Oberdiek
  • 271,626