1

I have a homework problem that I reuse relatively frequently, and I'm trying to update the solution to match the dynamic.

Suppose z(t) is a periodic function with period 3. Evaluate z(\the\year).

\begin{array}{c||c|c|c|c|c|c}
    t   &0 & 1 & 2 & 3 & \dotsb & \the\year\\
    \hline
    z(t)    &12 & 5\pi & -6 & 12 & \dotsb & ??
\end{array}

Is there any clever coding that can be used to automatically compute \the\year mod3 and \ifthenelse the result?

D.J.
  • 159

2 Answers2

1

If you're willing and able to use LuaLaTeX (instead of either pdfLaTeX or XeLaTeX), you can perform modular division on \the\year.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode} % for '\luaexec' macro % helper macro '\modulo' invokes Lua to perform the actual job \newcommand\modulo[2]{\luaexec{tex.sprint(#1 % #2)}}

\begin{document} \the\year, \modulo{\the\year}{3} \end{document}

Mico
  • 506,678
1

If you are happy with it outputting -1 instead of 2, then you can use TeX primitives.

\documentclass{article}
\newcommand\modcomp[2]{\the\numexpr #1 - (#1/#2)*#2\relax}

\begin{document} 10 mod 3 is \modcomp{10}{3}. 11 mod 3 is \modcomp{11}{3}.

\the\year\ mod 7 is \modcomp{\the\year}{7} \end{document}

enter image description here


If you insist on non-negative outputs, the TeX primitive \divide ... by truncates rather than rounds, so you can do

\documentclass{article}
\newcount\b
\newcount\n
\newcommand\modcomp[2]{\n=#1 \b=#2 \divide\n by \b \multiply\n by \b \the\numexpr#1 - \number\n\relax}

\begin{document} 10 mod 3 is \modcomp{10}{3}. 11 mod 3 is \modcomp{11}{3}.

\the\year\ mod 7 is \modcomp{\the\year}{7} \end{document}

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • Thank you! That's a nice solution, @willie-wong and then I can use \ifthenelse on that result. – D.J. Apr 12 '21 at 20:35
  • I just added a version with non-negative values. I forgot you were going to do \ifthenelse. With the non-negative output you can use \ifcase instead which has easier syntax than \ifthenelse for this application. See https://tex.stackexchange.com/a/17678/119 – Willie Wong Apr 12 '21 at 20:41