23

When I am faced with a simple linear congruence such as $$9x \equiv 7 \pmod{13}$$ and I am working without any calculating aid handy, I tend to do something like the following:

"Notice" that adding $13$ on the right and subtracting $13x$ on the left gives: $$-4x \equiv 20 \pmod{13}$$

so that $$x \equiv -5 \equiv 8 \pmod{13}.$$

Clearly this process works and is easy to justify (apart from not having an algorithm for "noticing"), but my question is this: I have a vague recollection of reading somewhere this sort of process was the preferred method of C. F. Gauss, but I cannot find any evidence for this now, so does anyone know anything about this, or could provide a reference? (Or have I just imagined it all?)

I would also be interested to hear if anyone else does anything similar.

Bill Dubuque
  • 272,048
Old John
  • 19,569
  • 3
  • 59
  • 113
  • I suppose we all have our own "ad hoc" methods. I would have "noticed" that multiplying by $3$ gives $x \equiv 21 \equiv 8$ (mod $13$). I have always believed that Gauss invented modular arithmetic. It is certainly discussed at length in Disquisitiones Arithmeticae, which I own a copy of. – Geoff Robinson Jul 24 '12 at 15:08
  • It's not clear to me that the process always works, but it is interesting. It seems to me that the trick is that we get to replace our equation and hope for common prime factors between the coefficients. – Andrew Jul 24 '12 at 15:09
  • @GeoffRobinson I am pretty sure Gauss did invent the notation, and I too have a copy of Disquisitiones Arithmeticae, but I cannot see anything in it along exactly the lines of my question, unfortunately. – Old John Jul 24 '12 at 15:13
  • It is not an algorithm in the precise sense of the word. The idea is basically to multiply or divide (on both sides) or add (or subtract) a multiple of the modulus (on either side) to get something which is equivalent, but "simpler". Repeating this vague process a number of times will give a solution (if the original has a solution) as at each step the multiple of $x$ on the left is going to get smaller, and the new congruence is equivalent to the previous one. – Old John Jul 24 '12 at 15:16
  • @Old John: Thanks. Yes Bill Dubuque's answer made what was going on reasonably clear. – Geoff Robinson Jul 24 '12 at 18:46
  • When the numbers are not large, many times I have calculated the inverse of $a$ directly calculating $a^{p-2}$ modulo $p$ by the successive steps because of $a^{p-1}=1$ (in general this steps are easy sometimes). – Piquito Mar 18 '20 at 02:07

5 Answers5

30

$bx\equiv a\pmod{\!m}$ has a unique solution $\!\iff\!b\,$ is coprime to the modulus $m$. If so, by Bezout $\,b\,$ is invertible $\!\bmod m,\,$ so scaling $\,bx\equiv a\,$ by $\,b^{-1}\,$ we obtain the unique solution $\,x\equiv b^{-1}a =: a/b.\,$ We can quickly compute $\,b^{-1}\pmod{\!m}\,$ by the extended Euclidean algorithm, but there are often more convenient ways for smaller numbers (e.g. here and here are a handful of methods applied). We describe a few such methods below, viewing $\, x\equiv b^{-1}a \equiv a/b\,$ as a modular fraction. [See here for the general method when the solution is not unique, i.e. when $\gcd(b,m)>1$].


The first, Gauss's algorithm, is based on Gauss's proof of Euclid's lemma via the descent $\,p\mid ab\,\Rightarrow\, p\mid a(p\bmod b).\,$ Generally it only works for prime moduli, but we can also execute the general extended Euclidean algorithm in fraction form too (using multi-valued "fractions").

It works by repeatedly scaling $\rm\:\color{#C00}{\frac{A}B}\overset{\times\ N} \to \frac{AN}{BN}\: $ by the least $\rm\,N\,$ with $\rm\, BN \ge 13,\, $ then reducing mod $13$

$$\rm\displaystyle \ mod\ 13\!:\,\ \color{#C00}{\frac{7}9} \,\overset{\times\ 2}\equiv\, \frac{14}{18}\, \equiv\, \color{#C00}{\frac{1}5}\,\overset{\times \ 3}\equiv\, \frac{3}{15}\,\equiv\, \color{#C00}{\frac{3}2} \,\overset{\times\ 7}\equiv\, \frac{21}{14} \,\equiv\, \color{#C00}{\frac{8}1}\qquad\!\! $$

Denominators of the $\color{#c00}{\rm reduced}$ fractions decrease $\,\color{#C00}{ 9 > 5 > 2> \ldots}\,$ so reach $\color{#C00}{1}\,$ (not $\,0\,$ else the denominator would be a proper factor of the prime modulus; it may fail for composite modulus)

Simpler: $ $ using $\rm\color{#0a0}{least}$ residues: $\displaystyle\ \ \frac{7}9\,\equiv\, \frac{7}{\!\color{#0a0}{-4}\!\ \,}\,\overset{\times\ 3}\equiv\,\frac{21}{\!\!-12\ \ \ \!\!}\,\equiv\, \color{#c00}{\frac{8}1}$

This optimization using $\rm\color{#0a0}{least\ magnitude}$ residues $\,0,\pm 1, \pm 2.\ldots$ often simplifies mod arithmetic. Here we can also optimize by (sometimes) cancelling obvious common factors, or by pulling out obvious factors of denominators, etc. For example

$$\frac{7}9\,\equiv\, \frac{\!-6\,}{\!-4\,}\,\equiv\frac{\!-3\,}{\!-2\,}\,\equiv\frac{10}{\!-2\,}\,\equiv\,-5$$

$$\frac{7}9\,\equiv\,\frac{\!-1\cdot 6}{\ \ 3\cdot 3}\,\equiv\,\frac{\!\,12\cdot 6\!}{\ \ \,3\cdot 3}\,\equiv\, 4\cdot 2$$


Or twiddle it as you did: $ $ check if quotient $\rm a/b\equiv (a\pm\!13\,i)/(b\pm\!13\,j)\,$ is exact for small $\rm\,i,j,\,$ e.g.

$$ \frac{1}7\,\equiv \frac{\!-12}{-6}\,\equiv\, 2;\ \ \ \frac{5}7\,\equiv\,\frac{18}{\!-6\!\,}\,\equiv -3$$

When working with smaller numbers there is a higher probability of such optimizations being applicable (the law of small numbers), so it's well-worth looking for such in manual calculations.

Generally we can choose a congruent numerator giving an exact quotient by Inverse Reciprocity.

$\bmod 13\!:\ \dfrac{a}{b}\equiv \dfrac{a-13\left[\color{#0a0}{\dfrac{a}{13}}\bmod b\right]}b\,\ $ e.g. $\,\ \dfrac{8}9\equiv \dfrac{8-13\overbrace{\left[\dfrac{8}{\color{#c00}{13}}\bmod 9\right]}^{\large\color{#c00}{ 13\ \,\equiv\,\ 4\ }}}9\equiv\dfrac{8-13[2]}9\equiv-2$

Note that the value $\,\color{#0a0}{x\equiv a/13}\,$ is exactly what we need to make the numerator divisible by $b,\,$ i.e.

$\qquad\quad\bmod b\!:\,\ a-13\,[\color{#0a0}x]\equiv 0\iff 13x\equiv a\iff \color{#0a0}{x\equiv a/13}$

This is essentially an optimization of the Extended Euclidean Algorithm (when it takes two steps).

Note $ $ Gauss' algorithm is my name for a special case of the Euclidean algorithm that's implicit in Gauss' Disquisitiones Arithmeticae, Art. 13, 1801. I don't know if Gauss explicitly used this algorithm elsewhere (apparently he chose to avoid use or mention of the Euclidean algorithm in Disq. Arith.). Gauss does briefly mention modular fractions in Art. 31 in Disq. Arith.

The reformulation above in terms of fractions does not occur in Gauss' work as far as I know. I devised it in my youth before I had perused Disq. Arith. It is likely very old but I don't recall seeing it in any literature. I'd be very grateful for any historical references.

See here for further discussion, including a detailed comparison with the descent employed by Gauss, and a formal proof of correctness of the algorithm.

Beware $ $ Modular fraction arithmetic is valid only for fractions with denominator coprime to the modulus. See here for further discussion.

Bill Dubuque
  • 272,048
  • Thanks for the reference - so it was Gauss after all. The Gauss process is basically what I do, although I keep an eye out for any possible shortcuts, as in the example I gave. – Old John Jul 24 '12 at 15:21
  • @mathh As the linked post says, Gauss's algorithm requires prime modulus. Generally modular fractions make sense only for denominators coprime to the modulus. Thus when scaling fractions we must restrict to scale factors coprime to the modulus, e.g. in your case we can do $\tag*{}$ ${\rm mod}\ 10!:\ \dfrac{1}3\equiv \dfrac{3}9\equiv \dfrac{3}{-1} \equiv -3\equiv 7\ \ $ – Bill Dubuque Aug 18 '14 at 16:44
  • I'm sorry, I deleted the comment before you replied since I found it out. I would add to the above explanations that the denominators can be reduced if and only if the modulus is coprime to them. – user26486 Aug 18 '14 at 16:51
  • @BillDubuque Is your notation $\mod p : a\equiv b$ as formal and accepted as $a\equiv b\pmod p$? I.e. do scientists, etc. use it? – user26486 Aug 18 '14 at 16:54
  • @mathh It is less commonly used. I find it is clearer to students since it specifies the context first (vs. last) in normal reading order (left-to-right). – Bill Dubuque Aug 18 '14 at 17:05
  • How do you prove that this algorithm always returns the answer for any prime modulus? – user26486 Feb 06 '15 at 19:05
  • @user314 As stated, the $\rm\color{#c00}{denominators}$ are decreasing, so they eventually reach $,\color{#c00}1,$ (never $,\color{#c00}0,,$ else the denominator would be a proper factor of the prime modulus). – Bill Dubuque Feb 06 '15 at 19:12
  • @BillDubuque What confuses here also is the chaining of the congruence sign. How can $9x = 7$ mod $13$ be congruent to $18x = 14$ mod $13$ etc etc. Chaining it by the above example you are essentially saying that $7 = 1$ mod $13$ which is obviously not true. – Michael Munta Jan 05 '19 at 08:10
  • I know it makes sense when you scale the congruence by the same coefficient then essentially the congruence is preserved, but the congruence that was scaled up is not congruent to the resulting congruence. – Michael Munta Jan 05 '19 at 10:41
  • @Michael The equivalent equations $,B,X\equiv A\iff BN,X\equiv AN,,$ necessarily have congruent solutions $, X\equiv A/B\equiv (AN)/(BN).,$ The fraction $,x/y,$ denotes the modular integer $,xy^{-1},$ so the congruence denotes a relation between numbers, not equations. It might help to consider the analogous statement for ordinary fractions (rational numbers). – Bill Dubuque Jan 05 '19 at 14:53
  • @BillDubuque So what does this scaling up and then reducing in modulo represent in Gauss's book? What property of modular arithmetic is achieving the reduction of the denominators? What do the denominators even represent in the reduction process? – Michael Munta Jan 05 '19 at 18:20
  • @Michael The denoms are a descending sequence of multiples of the initial denom. Look at the examples here. The first example (using all negative multipliers) is the way Gauss's proof works, using $,b\to b'=p\bmod b = p-qb,,$ i.e. $qb$ is the largest mutiple of $b$ that is $< p$, and $b'$ is how how much short it is from $p$. In clock arithmetic, start at $b$, take steps of length $b$ and go as far as you can without passing $p$. Then $b'$ is what "remains", the standard "remainder" – Bill Dubuque Jan 05 '19 at 22:46
  • The fraction method goes $1$ step further and chooses the first multiple $\ge p$, then the "excess" is the remainder (1st step in "quicker" method). It amounts only to a sign difference in the choice of remainder. e.g. $,43\equiv -17\pmod{!60}$ in the $2$ examples there. I'll give some examples when I can find some time to answer your question. – Bill Dubuque Jan 05 '19 at 22:47
  • I don't understand how negative multipliers appear since first you wrote them normally $103 = 60*1+43$, but in the next line it was $-1$. Every time I think I understand something then when you answer I get confused again. I am not seeing something essential in this algorithm which is preventing me from understanding it. :( – Michael Munta Jan 06 '19 at 10:15
  • @Michael As above, $\bmod 103,,$ the largest multiple of $60$ that is $< 103$ is $1\cdot 60$, with remainder $43$, i.e. $,1\cdot 60\equiv -43,$ so negating: $(-1)60\equiv 43$ is a smaller multiple of $60$. Another smaller multiple is the first multiple $> 103$ which is $,2\cdot 60\equiv 17,,$ by going one step further ($\times 2, $ vs. $\times 1).,$ Being one step away $, {-}43+60 = 17,$ so $,-43\equiv 17\pmod{!60},$ i.e. the methods differ only by sign. – Bill Dubuque Jan 06 '19 at 18:34
  • Choosing the smaller $17<43,$ generally simplifies arithmetic. It corresponds to using least-magnitude remainders $r = b\bmod 60,,$ where $,-29\le r \le 30,,$ hence $,104\bmod{!60}= -17,,$ not $43.,$ But we are free to choose either (e.g. a larger denominator may be "simpler" if it makes the fraction an exact quotient). Generally whenever a problem has reflection symmetry (here negation, i.e. inverting $a$ is equivalent to inverting $-a$) we can often exploit this symmetry to simplify matters (here choosing the sign that makes the inversion computation simplest). – Bill Dubuque Jan 06 '19 at 19:09
  • This is too hard for me to comprehend without a thorough example and correspondence to Gauss. I will wait for an answer on my question. – Michael Munta Jan 08 '19 at 16:31
  • @Michael I fear an answer will fare no better unless you can tell me more precisely where the difficulty lies (e.g. you might need more practice with modular arithmetic). – Bill Dubuque Jan 08 '19 at 16:52
  • What I want to understand is how you pulled this out of Gauss's text because all of my questions are scattered around and I am lost now. That is why I ask for an answer where I can see what happens from beginning to the end. I look at the fractional form and I see that it works, but when you say it is essentially Gauss I am confused. I want to know what essentially went on in your head when you observed that so I can connect all the dots. I can not google for your method because this is something you observed and you are available only on this website. :) – Michael Munta Jan 08 '19 at 17:14
  • Actually I didn't "pull it out of Gauss's text". I invented it in my youth to simplify my hand computations, then later when perusing Disq. Arith. I noticed that it is essentially the same descent that Gauss used there. If you can be more precise about where the difficulties lie then that would be helpful. Else I have to reread this long discussion and make guesses about what points need clarification, and that is a very inefficient way to proceed (hence the delay answering). – Bill Dubuque Jan 08 '19 at 17:28
  • Then what I want to know is how is this the equivalent descent to Gauss. Can you show via example what you are doing and how it relates to Gauss? Something like in your sci.math post, but with a fractional counterpart and a bit more explicit. For example when you say it generates a descending chain of $a$ mod $p$ please show it with actual numbers. – Michael Munta Jan 08 '19 at 18:14
  • @BillDubuque So the example that you gave here is the same thing as your sci.math post like this: $923*7 \equiv 378$ (mod $13$). And $378$ is the inverse of $9$. Sequence of multiples of 9 is: $18$, $54$, $378$. Is that right? – Michael Munta Jan 15 '19 at 15:24
  • 1
    @Michael Yes, it shows $\bmod 13!:\ 9^{-1}\equiv \color{#0a0}2\times\color{#0a0}3\times\color{#0a0}7\equiv 3.\ $ The sequence of decreasing multiples of $9$ is comprised by the reduced denominators (in red in the answer), i.e. as below – Bill Dubuque Jan 15 '19 at 15:51
  • 2
    $${\begin{align}&9\ \overset{\large \times\color{#0a0} 2}\longrightarrow\ 5\overset{\large \times\color{#0a0} 3}\longrightarrow 2,\overset{\large \times\color{#0a0} 7}\longrightarrow, 1\[.4em] \Rightarrow\ \ & 9\ \ \times\ \ \color{#0a0}2\ \times\ \color{#0a0}3\ \times\ \color{#0a0}7\equiv 1\[.4em] \Rightarrow\ \ &9^{-1}!\equiv \color{#0a0}2\ \times\ \color{#0a0}3\ \times\ \color{#0a0}7\equiv 3& \end{align}\quad\qquad}$$ – Bill Dubuque Jan 15 '19 at 15:51
  • Finally I understand, thank you for your help. – Michael Munta Jan 15 '19 at 15:56
  • One more thing. If following directly Gauss's algorithm and your sci.math post $(p $ mod $a, p)$, then it looks like this and much simpler: $13 = 91 + 4 = 43 + 1$ and therefore $9 * 1 * 3 \equiv 27 \equiv 1$ (mod $13$) or also $9^{-1} \equiv 1 * 3 \equiv 3$. Is this correct? What do you achieve with what you did with fractions? – Michael Munta Jan 15 '19 at 18:13
  • @BillDubuque I mean specifically 'scaling by the least $N$ to make it greater than modulus'. How does that make it better than what I have shown in my previous comment? – Michael Munta Jan 15 '19 at 20:20
  • @Michael Yes it is correct and is explained in the above answer (see "Or, simpler..", using least magnitude residues). Such optimizations aren't needed in Gauss's proof because (essentially) it only requires the existence of an inverse, so there is no need to be concerned with the efficiency of computing the inverse. – Bill Dubuque Jan 15 '19 at 21:10
  • @BillDubuque I'm coming back to this to clear some doubts. I have been trying again to see what actually happens here. To me it seems that this fractional form has nothing to do with Gauss's proof since you reduce the coefficient mod $p$ and Gauss does it mod $b$. The only properties you use are the fact that you can multiply both sides by the same coefficient and that you can reduce the factors in modulo. If it were following Gauss we would have $9x \equiv 7, 18x \equiv 14, \color{red}{-5x} \equiv 14, -15x \equiv 42...$. That would be exactly what he did $a(p - mb)$. What is the difference? – Michael Munta Feb 06 '19 at 11:29
  • So as you said before Gauss explains the congruence when $bx \equiv 0$, but here we have $bx \equiv y$. Then Gauss scales the congruence by some $m$ on both sides and in your case it is $2$(first step). After that the coefficient is simply replaced via distributivity by $p mod b$ or $(p - mb)$ -> $(13 - 2*9)$ which is $-5$ in my comment. You get a positive $5$ reducing it mod $13$, but it should not be like that. Also if we scaled the congruence by $m=1$ it would be faster. Why is it that in Gauss's method all signs are opposite to yours? – Michael Munta Feb 06 '19 at 12:43
  • @BillDubuque Hi Bill. I have recently reread all the various posts about Gauss' algorithm and I have one more thing I'd like you to confirm. The answer you have posted here https://math.stackexchange.com/questions/3059260/gausss-algorithm-for-computing-modular-fractions-and-inverses is the actual implicit algorithm from the book, namely, the (p mod a,p) descent and it is translated to all these different forms. This is what I have asked you to demonstrate and you did that. However, the answer on this page is in fact the algorithm you invented without Gauss. Right? – Michael Munta Sep 10 '20 at 09:23
  • My question is why did you later attribute it to Gauss when the algorithm is not really the same thing as the process you demonstrated in the above mentioned question? Since there is no (p mod a,p) descent in it and it uses just the modular arithmetic properties rather than something from the book. – Michael Munta Sep 10 '20 at 09:23
3

When the prime is a reasonably small one I'd rather find directly the inverse: $$9^{-1}=\frac{1}{9}=3\pmod {13}\Longrightarrow 9x=7\Longrightarrow x=7\cdot 9^{-1}=7\cdot 3= 21=8\pmod {13}$$ But...I try Gauss's method when the prime is big and/or evaluating inverses is messy.

DonAntonio
  • 211,718
  • 17
  • 136
  • 287
1

9x = 7 mod 13

9x = 7 + 13n

9x = 20 for n = 1

9x = 33 for n = 2

9x = 46 for n = 3

9x = 59 for n = 4

9x = 72 for n = 5

Then x = 8 mod 13

You arrive at the correct answer before n = 13.

0

Another offbeat process but with algorithmic potential.

Solve $9x \equiv 7 \pmod{13}$.

$\quad 9x = 7 + 13y \implies 0 \equiv 1 + y \pmod{3} \implies y \equiv 2 \pmod{3}$

and

$\quad y : 2 \; \mid \; 7 + 13y = 33 \quad \quad \text{NO GOOD!}$
$\quad y : 5 \; \mid \; 7 + 13y = 72 \quad \quad \text{AND is divisible by } 9$

So,

$\tag{ANS} x \equiv 8 \pmod{13}$

CopyPasteIt
  • 11,366
0

When presented with

$\tag 1 ax \equiv b \pmod{n}$

if $a \mid b$ the solution is right in front of you.

But there is also a 'plug in' solution if $a \mid n-1$ or $a \mid n+1$:

If $a \mid n-1$ then $x = \Large(\frac{n-1}{a}) \normalsize (-b)$ solves $\text{(1)}$.

If $a \mid n+1$ then $x = \Large(\frac{n+1}{a}) \normalsize (b)$ solves $\text{(1)}$.

Can we 'make hay' with the OP's linear congruence?

$\quad 9x \equiv 7 \pmod{13} \; \text{ iff } \; -4x \equiv 7 \pmod{13} \; \text{ iff }$
$ \quad 4x \equiv -7 \pmod{13} \; \text{ iff } \; 4x \equiv 6 \pmod{13}$

We are in business now with $4x \equiv 6 \pmod{13}$ since $4 \mid 12$; a solution is

$\quad x = \Large(\frac{n-1}{a}) \normalsize (-b) = (3)(-6) = -18 \equiv 8 \pmod{13}$


Here is an example where the $n + 1$ manipulation can be used:

$\quad 5x \equiv 1 \pmod{17} \; \text{ iff } \; -12x \equiv 1 \pmod{17} \; \text{ iff }$
$ \quad 12x \equiv -1 \pmod{17} \; \text{ iff } \; 12x \equiv 16 \pmod{17} \; \text{ iff } \; 6x \equiv 8 \pmod{17}$

We are in business now with $6x \equiv 8 \pmod{17}$ since $6 \mid 18$; a solution is

$\quad x = \Large(\frac{n+1}{a}) \normalsize (b) = (3)(8) = 24 \equiv 7 \pmod{17}$

CopyPasteIt
  • 11,366