TITLE: Modular Arithmetic in LaTeX
QUESTION [19 upvotes]: This question may end up [closed], but I'm going to ask and let the people decide.  It's certainly the kind of question that I'd ask people at tea, and it's not one whose answer I've been able find with Google.
TeX, I have heard, is Turing complete.  In theory, this means that we can do modular arithmetic with LaTeX programs.  I'd like to know how this can be done in practice.
Background:  I've been using the \foreach command in TikZ to draw NxN arrays of nodes, indexed by pairs of integers (m,n).  I'd like to be able to use modular arithmetic and an ifthenelse statement to put different decorations on the nodes, depending on the value of (m+n) mod p.  Obviously, one can just do this by hand.  But that's not the world I want to live in.

REPLY [5 votes]: I've had to implement a lot of code in TeX while writing a package for drawing spectral sequences (sseq.sty). pgf makes computations in TeX easier, but if you really have a significant amount of code, I recommend you take a look at luatex/lualatex, which is included in many distributions and merges TeX with an interpreter for the easy-and-fast language Lua, which you can learn in a couple of hours. It's much more readable and speeds up the typesetting enormously.
TeX may be Turing complete (LaTeX as well, by the way), but so is a Turing machine, and you wouldn't want to implement modular arithmetic on a Turing machine, would you?