I don't want to display the modulo symbol, I want to programmatically calculate n modulo 3 and display the result.
8 Answers
You can also use \intcalcMod from the intcalc package:
\documentclass{article}
\usepackage{amsmath}
\usepackage{ifthen}
\usepackage{intcalc}
\newcounter{mycount}
\newcommand\Nmodiii[1]{%
\setcounter{mycount}{0}\whiledo{\value{mycount}<#1}
{$\themycount\pmod 3=\intcalcMod{\value{mycount}}{3}$\\\stepcounter{mycount}}
}
\begin{document}
\noindent A little example: $8\pmod 3=\intcalcMod{8}{3}$
\noindent And a little loop:\\
\Nmodiii{20}
\end{document}

The code that appears in the link posted in a comment, there are some spurious blank spaces producing an undesired indentation of the first line; here's a corrected version:
\documentclass{article}
\usepackage{ifthen}
\usepackage{forloop}
\usepackage{fmtcount}
\usepackage{intcalc}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
\newcounter{i}
\noindent\forloop{i}{1}{\value{i} < 101}{%
\ifthenelse{\equal{\intcalcMod{\value{i}}{15}}{0}}{
FizzBuzz
}{%
\ifthenelse{\equal{\intcalcMod{\value{i}}{3}}{0}}{
Fizz
}{%
\ifthenelse{\equal{\intcalcMod{\value{i}}{5}}{0}}{
Buzz
}{%
\thei
}
}
}\\
}
\end{multicols}
\end{document}
-
! Missing number, treated as zero. <to be read again> v l.32 }mcandre– mcandre2011-11-11 01:13:44 +00:00Commented Nov 11, 2011 at 1:13 -
Thanks, that helps. I'm writing FizzBuzz for LaTeX, so I need to use the mod result in further calculations, not just display it.mcandre– mcandre2011-11-11 01:14:20 +00:00Commented Nov 11, 2011 at 1:14
-
@mcandre: nothing prevents you from using the result in your calculations!Gonzalo Medina– Gonzalo Medina2011-11-11 01:15:14 +00:00Commented Nov 11, 2011 at 1:15
-
Hmm. When I do this, I get
! Missing number, treated as zero.github.com/mcandre/mcandre/blob/master/latex/fizzy.texmcandre– mcandre2011-11-11 01:30:07 +00:00Commented Nov 11, 2011 at 1:30 -
You are using "value" in your code and you should use
\value(with a backslash). Also, you could have included that code in an edit to your original question.Gonzalo Medina– Gonzalo Medina2011-11-11 01:34:34 +00:00Commented Nov 11, 2011 at 1:34
There are several nice answers using different packages. I'd like to note that TeX uses integer arithmetics, so it is easy to program the standard formula a-(a/b)*b, where / means integer division.
Plain TeX solution:
\newcount\tmpcnta
\def\modulo#1#2{\tmpcnta=#1
\divide\tmpcnta by #2
\multiply\tmpcnta by #2
\multiply\tmpcnta by -1
\advance\tmpcnta by #1\relax
\the\tmpcnta}
\modulo{17}{3}
\modulo{19}{3}
\bye
LaTeX solution:
\documentclass{article}
\makeatletter
\newcommand\modulo[2]{\@tempcnta=#1
\divide\@tempcnta by #2
\multiply\@tempcnta by #2
\multiply\@tempcnta by -1
\advance\@tempcnta by #1\relax
\the\@tempcnta}
\makeatother
\begin{document}
\modulo{17}{3}
\modulo{19}{3}
\end{document}
-
I used a similar solution in a calendar.starblue– starblue2011-11-11 14:21:48 +00:00Commented Nov 11, 2011 at 14:21
-
1Why can't you write
\divide#1 by #2or\divide\number\numexpr#1\relax by #2?A.Ellett– A.Ellett2013-12-19 07:52:54 +00:00Commented Dec 19, 2013 at 7:52 -
1@A.Ellett
\dividemutates (changes) the thing being divided (as do\multiplyand\advance), and so we want to change our own counter\tmpcntarather than#1.ShreevatsaR– ShreevatsaR2017-11-02 20:30:13 +00:00Commented Nov 2, 2017 at 20:30 -
How can I check with
ifthenelse{\equal{\modulo{17}{3}}{0}}{MOD 0}{NOT 0}This seems not to work...Pascal– Pascal2024-07-27 10:18:11 +00:00Commented Jul 27, 2024 at 10:18
The fp package is small and provides the functionality to do quite complex arithmetic. In the minimal example below the macro \modulo{<a>}{<b>} stores the result of <a> mod <b> in the macro \result, which is then directly printed:

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\newcommand{\modulo}[2]{%
\FPeval{\result}{trunc(#1-(#2*trunc(#1/#2,0)),0)}\result%
}
\begin{document}
Some modular arithmetic:
\begin{itemize}
\item $512 \pmod{7}=\modulo{512}{7}$
\item $6 \pmod{4}=\modulo{6}{4}$
\item $15 \pmod{4}=\modulo{15}{4}$
\item $1234567 \pmod{3}=\modulo{1234567}{3}$
\end{itemize}
\end{document}
Since the result is stored in \result, it can be used later in the text as well, until another execution of \modulo will overwrite \result.
Similar functionality in terms of mathematical functions is provided with pgf as well.
-
! Undefined control sequence. \FP@@upn ...on \string "#2\string "}\edef \FP@tmp {[#2]}\expandafter \FP@upn... l.38 }mcandre– mcandre2011-11-11 00:42:40 +00:00Commented Nov 11, 2011 at 0:42 -
Do you receive this error when compiling my MWE?2011-11-11 00:47:10 +00:00Commented Nov 11, 2011 at 0:47
-
Yes. Specs: TeXworks on Mac OS X 10.7.2.mcandre– mcandre2011-11-11 00:53:18 +00:00Commented Nov 11, 2011 at 0:53
-
What distribution of TeX do you have installed? I have TeX Live 2011 with
fpverion1995/04/02.2011-11-11 00:58:33 +00:00Commented Nov 11, 2011 at 0:58 -
2I like that a LaTeX solution to doing modular arithmetic is to load a package designed to do floating point calculations. WHAT!?Seamus– Seamus2012-02-02 17:08:15 +00:00Commented Feb 2, 2012 at 17:08
The "expandable" version, using e-TeX's \numexpr:
\def\truncdiv#1#2{((#1-(#2-1)/2)/#2)}
\def\moduloop#1#2{(#1-\truncdiv{#1}{#2}*#2)}
\def\modulo#1#2{\number\numexpr\moduloop{#1}{#2}\relax}
\truncdiv and \moduloop can be plugged into other expressions. It's necessary to do like this because \numexpr performs rounded integer division.
-
the same idea was used in the
calendarweekTeX package.ogerard– ogerard2013-06-05 09:27:18 +00:00Commented Jun 5, 2013 at 9:27 -
2Hmm, from
\truncdiv{0}{64}I get -1 and so\modulo{0}{64}gives 64.ShreevatsaR– ShreevatsaR2017-11-02 20:26:28 +00:00Commented Nov 2, 2017 at 20:26 -
1I'm using the following for now, which works for positive
#2:\def\moduloop#1#2{\ifnum \numexpr(#1 - (#1/#2)*(#2))\relax < 0 (#1 - (#1/#2)*(#2) + #2) \else (#1 - (#1/#2)*(#2)) \fi}and\def\truncdiv#1#2{((#1 - \moduloop{#1}{#2})/(#2))}ShreevatsaR– ShreevatsaR2017-11-02 20:44:58 +00:00Commented Nov 2, 2017 at 20:44
LaTeX3 (the expl3 package) also has a facility for computing modulus (moduli?), namely \int_mod:nn.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\newcommand{\mymod}[2]{\int_mod:nn{#1}{#2}}
\ExplSyntaxOff
\begin{document}
The residue of $45$ modulo $19$ is $\mymod{45}{19}$.
\end{document}
-
1Modulus is (most likely) the diminutive of modus, therefore should -us/-i, not -us/-us.MaestroGlanz– MaestroGlanz2025-01-05 08:30:35 +00:00Commented Jan 5, 2025 at 8:30
Another solution is to use pgfmath
\documentclass{article}
\input{pgfutil-common.tex}
\usepackage{pgfkeys,pgfmath}
\begin{document}
\pgfmathparse{mod(20,6)} \pgfmathresult %displays 2.0
\pgfmathtruncatemacro{\myint}{ \pgfmathresult}
\myint %displays 2
\end{document}
-
It seems that only the package
pgfmathis necessary.\input{pgfutil-common.tex}and\usepackage{pgfkeys}could be removed.dexteritas– dexteritas2020-05-24 20:35:44 +00:00Commented May 24, 2020 at 20:35
You can use the calculator package then, type this code:
\MODULO{14}{3}{\sol}
$14\pmod{3}=\sol$
-
Nice, a new package! But you might want to make it clearer that you are the package author...cgnieder– cgnieder2012-06-12 10:35:42 +00:00Commented Jun 12, 2012 at 10:35
-
How nice!
:)Feel free to add your package to our list. And by the way, welcome to TeX.sx!:)Paulo Cereda– Paulo Cereda2012-06-12 10:54:12 +00:00Commented Jun 12, 2012 at 10:54
You may use calc package as long as the absolute values of the numbers are not exceeding 2^31-1=2147483647. Otherwise you may use bigintcalc package.
\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\newcounter{modulo}
\newcommand\modulo[2]{%
\setcounter{modulo}{#1-(#1/#2)*#2}%
\arabic{modulo}%
}
\begin{document}
\begin{align*}
131 \equiv \modulo{131}{3} &\pmod{3} \\
131 \equiv \modulo{131}{5} &\pmod{5} \\
131 \equiv \modulo{131}{7} &\pmod{7} \\
131 \equiv \modulo{131}{8} &\pmod{8} \\
-97 \equiv \modulo{-97}{3} &\pmod{3} \\
-97 \equiv \modulo{-97}{5} &\pmod{5} \\
-97 \equiv \modulo{-97}{7} &\pmod{7} \\
-97 \equiv \modulo{-97}{8} &\pmod{8} \\
\end{align*}
\end{document}
