5

I want to avoid using \tmp. Is it possible to remove it while keeping the same output?

enter image description here

% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
\newcount\x
\newcount\tmp

% Initializing.
\pages=9% Try it for an odd or even integer.
\tmp=\pages
\advance\tmp by 1
\x=1

\loop
    \ifnum\x<\tmp
    \noindent\hfill\the\x
    \advance\x by 1
    \ifnum\x>\pages
        \hfill\null\endgraf
    \else
        \hfill\the\x\hfill\null\endgraf
    \fi
    \advance\x by 1
\repeat

\bye

Note: Please don't use e-TeX extension as I am learning Knuth's original TeX right now.

4 Answers4

5

Start with \x=0, step it as soon as possible and invert the inner conditional:

\newcount\pages % \pages is given so you cannot remove it!
\newcount\x

% Initializing.
\pages=9 % Try it for an odd or even integer.
\x=0

\loop
  \ifnum\x<\pages
  \advance\x by 1
  \noindent\hfill\the\x
  \ifnum\x<\pages
    \advance\x by 1
    \hfill\the\x
  \fi
  \hfill\null\endgraf
\repeat

\bye
egreg
  • 1,121,712
4

What's wrong with simply testing for equality at the end of the loop:

% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
\newcount\x

% Initializing.
\pages=9 % Try it for an odd or even integer.

\x=1 %

\loop
  \ifnum\x=\pages
    \noindent
    \hfill
    \number\x
    \hfill
    \null
    \endgraf
  \fi
  \ifnum\x<\pages
    \noindent
    \hfill
    \number\x
    \advance\x by 1 %
    \hfill
    \number\x
    \hfill
    \null
    \endgraf
    \advance\x by 1 %
\repeat

\bye

(I've added in a few spaces to correctly terminate numbers.)

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
4

You got rid of \tmp in your answer, this removes \x as well.

% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!

% Initializing.
\pages=9% Try it for an odd or even integer.


\def\z#1{%
\ifnum#1<\numexpr\pages+1\relax
    \noindent\hfill#1%
  \ifnum#1=\pages
        \hfill\null\endgraf
    \else
        \hfill\number\numexpr#1+1\hfill\null\endgraf
    \fi
    \expandafter\z\expandafter{\number\numexpr#1+2\expandafter\relax\expandafter}%
\fi}
\z1
\bye
David Carlisle
  • 757,742
  • Who can compile it? I cannot. – kiss my armpit Jul 06 '12 at 18:37
  • 1
    @HiggsBoson Works for me (needs the e-TeX extensions, of course) – Joseph Wright Jul 06 '12 at 18:48
  • @David: Please why did you use \repeat in place of \fi. If I insert \loop in your conditionals, I will end up with a mess. Also, braces aren't expandable. So you have more than necessary number of \expandafter. I withdrew from editing your answer, but my name is still hanging in there. – Ahmed Musa Jul 07 '12 at 01:16
  • \repeat is a mistake forgot to change it from the OP's version, of course it worked as it is defined to be \fi but would break a loop as you say. Will edit. The \expandafter is not expanding the brace but the token after it, it is needed to expand the \fi/\repeat token before iteration. – David Carlisle Jul 07 '12 at 10:24
2

Using \numexpr\pages+1\relax seems to work:

Code:

% Declare counters.
\newcount\pages% \pages is given so you cannot remove it!
\newcount\x
%\newcount\tmp

% Initializing. \pages=9% Try it for an odd or even integer. %\tmp=\pages %\advance\tmp by 1 \x=1

\loop \ifnum\x<\numexpr\pages+1\relax \noindent\hfill\the\x \advance\x by 1 \ifnum\x>\pages \hfill\null\endgraf \else \hfill\the\x\hfill\null\endgraf \fi \advance\x by 1 \repeat

\bye

Peter Grill
  • 223,288
  • No performance drawback? – kiss my armpit Jul 06 '12 at 18:14
  • @HiggsBoson: I don't know. That would have to be measured. Just like any other software I would assume that if you are reducing your memory requirements, you are probably increasing the compute time. – Peter Grill Jul 06 '12 at 18:48
  • Judging performance with these things can be complex: assignments have a time requirement but so do expressions and expansion, so you have to test the real use cases. Lots of assignments will normally be slower than a small number of expressions, but one assignment versus one expression is more difficult! – Joseph Wright Jul 06 '12 at 18:50