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.

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
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.)

  • \pages=1 does not work. :-) – kiss my armpit Jul 6 '12 at 18:24
  • @HiggsBoson I've updated the answer to cover the no-loop case. – Joseph Wright Jul 6 '12 at 18:30
  • Defining \printpage seems to be unnecessary. Why don't you put its body directly into the first \ifnum? – kiss my armpit Jul 6 '12 at 19:01
  • 1
    @HiggsBoson Oops: I was experimenting with a few approaches, and forgot that I could remove that auxiliary! – Joseph Wright Jul 6 '12 at 19:02
  • Sorry, I switched to egreg's solution because of the minimal number of used characters. – kiss my armpit Jul 7 '12 at 9:39
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
  • Who can compile it? I cannot. – kiss my armpit Jul 6 '12 at 18:37
  • 1
    @HiggsBoson Works for me (needs the e-TeX extensions, of course) – Joseph Wright Jul 6 '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 7 '12 at 1: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 7 '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
  • No performance drawback? – kiss my armpit Jul 6 '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 6 '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 6 '12 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.