← 学习库 Discrete Mathematics (Levin) · 中英对照 目录

5.2 Introduction to Number Theory 数论导引

本页译自 LibreTexts · Discrete Mathematics (Levin) 第 5.2 章。公式经 MathJax 渲染,自定义宏已注入。

We have used the natural numbers to solve problems. This was the right set of numbers to work with in discrete mathematics because we always dealt with a whole number of things. The natural numbers have been a tool. Let's take a moment now to inspect that tool. What mathematical discoveries can we make about the natural numbers themselves?

我们在离散数学中一直用自然数来解题。自然数之所以合适,是因为我们处理的对象数量总是整数。自然数一直是我们的工具。现在花点时间审视一下这个工具:关于自然数本身,我们能做出哪些数学发现?

This is the main question of number theory: a huge, ancient, complex, and above all, beautiful branch of mathematics. Historically, number theory was known as the Queen of Mathematics and was very much a branch of pure mathematics, studied for its own sake instead of as a means to understanding real world applications. This has changed in recent years however, as applications of number theory have been unearthed. Probably the most well known example of this is RSA cryptography, one of the methods used in encrypt data on the internet. It is number theory that makes this possible.

这正是数论的核心问题——数论是数学中一个庞大、古老、复杂,更重要的是极为优美的分支。历史上,数论被称为「数学皇后」,长期属于纯粹数学,人们研究它是为了数学本身,而非将其当作理解现实应用的手段。然而近年来情况发生了变化,数论的应用被不断发掘。其中最著名的例子大概是 RSA 密码体制,它是互联网上用于加密数据的方法之一。正是数论使这一切成为可能。

What sorts of questions belong to the realm of number theory? Here is a motivating example. Recall in our study of induction, we asked:

哪些问题属于数论的范畴?下面用一个引例说明。回想我们在研究数学归纳法时曾问过:

Which amounts of postage can be made exactly using just 5-cent and 8-cent stamps?

仅用 5 分和 8 分邮票,能恰好凑出哪些面额?

We were able to prove that any amount greater than 27 cents could be made. You might wonder what would happen if we changed the denomination of the stamps. What if we instead had 4- and 9-cent stamps? Would there be some amount after which all amounts would be possible? Well, again, we could replace two 4-cent stamps with a 9-cent stamp, or three 9-cent stamps with seven 4-cent stamps. In each case we can create one more cent of postage. Using this as the inductive case would allow us to prove that any amount of postage greater than 23 cents can be made.

我们证明了大于 27 分的任意面额都能凑出。你或许会想,如果改变邮票的面值会怎样?假如改用 4 分和 9 分邮票呢?是否也存在某个数额,使得超过它的所有面额都能凑出?同样,我们可以用两张 4 分邮票换成一张 9 分邮票,或者用三张 9 分邮票换成七张 4 分邮票。每种换法都能多凑出 1 分邮资。以此作为归纳步骤,就能证明大于 23 分的任意面额都能凑出。

What if we had 2-cent and 4-cent stamps. Here it looks less promising. If we take some number of 2-cent stamps and some number of 4-cent stamps, what can we say about the total? Could it ever be odd? Doesn't look like it.

如果改用 2 分和 4 分邮票呢?这看起来希望不大。取若干张 2 分邮票和若干张 4 分邮票,总和会是什么样?能是奇数吗?似乎不能。

Why does 5 and 8 work, 4 and 9 work, but 2 and 4 not work? What is it about these numbers? If I gave you a pair of numbers, could you tell me right away if they would work or not? We will answer these questions, and more, after first investigating some simpler properties of numbers themselves.

为什么 5 和 8 可行、4 和 9 可行,而 2 和 4 不可行?这些数之间有什么玄机?如果给你一对数,你能立刻判断它们是否可行吗?在考察数本身的一些更简单的性质之后,我们将回答这些问题以及更多问题。

Divisibility 整除

It is easy to add and multiply natural numbers. If we extend our focus to all integers, then subtraction is also easy (we need the negative numbers so we can subtract any number from any other number, even larger from smaller). Division is the first operation that presents a challenge. If we wanted to extend our set of numbers so any division would be possible (maybe excluding division by 0) we would need to look at the rational numbers (the set of all numbers which can be written as fractions). This would be going too far, so we will refuse this option.

自然数相加、相乘都很容易。如果把范围扩展到全体整数,减法也不难(我们需要负数,这样才能用任意一个数减去另一个数,哪怕是用小的减大的)。除法是第一个带来挑战的运算。如果我们想扩展数的集合,使任意除法都可行(也许要排除除以 0),那就得考虑有理数(即所有能写成分数的数组成的集合)。但这未免走得太远,因此我们拒绝这一选择。

In fact, it is a good thing that not every number can be divided by other numbers. This helps us understand the structure of the natural numbers and opens the door to many interesting questions and applications.

事实上,并非每个数都能被别的数整除,这是一件好事。它帮助我们理解自然数的结构,并为许多有趣的问题与应用打开了大门。

If given numbers $a$ and $b$, it is possible that $a \div b$ gives a whole number. In this case, we say that $b$ divides $a$, in symbols, we write $b \mid a$. If this holds, then $b$ is a divisor or factor of $a$, and $a$ is a multiple of $b$. In other words, if $b \mid a$, then $a = bk$ for some integer $k$ (this is saying $a$ is some multiple of $b$).

给定数 $a$ 与 $b$,若 $a \div b$ 得到一个整数,则称 $b$ 整除 $a$;用符号表示,记作 $b \mid a$。若成立,则 $b$ 是 $a$ 的因数(或因子),$a$ 是 $b$ 的倍数。换言之,若 $b \mid a$,则存在整数 $k$ 使得 $a = bk$(即 $a$ 是 $b$ 的某个倍数)。

The Divisibility Relation

整除关系

Given integers $m$ and $n$, we say “$m$ divides $n$” and write

给定整数 $m$ 与 $n$,若 $n \div m$ 为整数,则称「$m$ 整除 $n$」,写作
\begin{equation*} m \mid n \end{equation*}
(上式表示 $m$ 整除 $n$。)

provided $n \div m$ is an integer. Thus the following assertions mean the same thing:

即 $n \div m$ 为整数。因此,以下各说法含义相同:
  1. $m \mid n$
  2. $n = mk$ for some integer $k$
  3. $m$ is a factor (or divisor) of $n$
  4. $n$ is a multiple of $m$.
  1. $m \mid n$
  2. 存在整数 $k$ 使 $n = mk$
  3. $m$ 是 $n$ 的因数(或除数)
  4. $n$ 是 $m$ 的倍数。

Notice that $m \mid n$ is a statement. It is either true or false. On the other hand, $n \div m$ or $n/m$ is some number. If we want to claim that $n/m$ is not an integer, so $m$ does not divide $n$, then we can write $m \nmid n$.

注意,$m \mid n$ 是一个命题,非真即假。另一方面,$n \div m$ 或 $n/m$ 是某个数值。若想断言 $n/m$ 不是整数(即 $m$ 不整除 $n$),可写作 $m \nmid n$。

Example 1

示例 1

Decide whether each of the statements below are true or false.

判断下列各命题的真假。
  1. $4 \mid 20$
  2. $20 \mid 4$
  3. $0 \mid 5$
  4. $5 \mid 0$
  5. $7 \mid 7$
  6. $1 \mid 37$
  7. $-3 \mid 12$
  8. $8 \mid 12$
  9. $1642 \mid 136299$
  1. $4 \mid 20$(4 整除 20)
  2. $20 \mid 4$(20 整除 4)
  3. $0 \mid 5$(0 整除 5)
  4. $5 \mid 0$(5 整除 0)
  5. $7 \mid 7$(7 整除 7)
  6. $1 \mid 37$(1 整除 37)
  7. $-3 \mid 12$(−3 整除 12)
  8. $8 \mid 12$(8 整除 12)
  9. $1642 \mid 136299$(1642 整除 136299)

Solution

解答
  1. True. 4 “goes into” 20 five times without remainder. In other words, $20 \div 4 = 5$, an integer. We could also justify this by saying that $20$ is a multiple of 4: $20 = 4\cdot 5$.
  2. False. While 20 is a multiple of 4, it is false that $4$ is a multiple of 20.
  3. False. $5 \div 0$ is not even defined, let alone an integer.
  4. True. In fact, $x \mid 0$ is true for all $x$. This is because 0 is a multiple of every number: $0 = x\cdot 0$.
  5. True. In fact, $x \mid x$ is true for all $x$.
  6. True. 1 divides every number (other than 0).
  7. True. Negative numbers work just fine for the divisibility relation. Here $12 = -3 \cdot 4$. It is also true that $3 \mid -12$ and that $-3 \mid -12$.
  8. False. Both 8 and 12 are divisible by 4, but this does not mean that $12$ is divisible by $8$.
  9. False. See below.
  1. 真。4 整除 20 五次而无余数。换言之,$20 \div 4 = 5$ 是整数。也可这样论证:20 是 4 的倍数,即 $20 = 4\cdot 5$。
  2. 假。虽然 20 是 4 的倍数,但「4 是 20 的倍数」不成立。
  3. 假。$5 \div 0$ 甚至没有定义,更谈不上整数。
  4. 真。事实上,对任意 $x$ 都有 $x \mid 0$。这是因为 0 是任何数的倍数:$0 = x\cdot 0$。
  5. 真。事实上,对任意 $x$ 都有 $x \mid x$。
  6. 真。1 整除所有(非 0 的)数。
  7. 真。负数在整除关系下同样适用。此处 $12 = -3 \cdot 4$。同时 $3 \mid -12$ 与 $-3 \mid -12$ 也都成立。
  8. 假。8 与 12 都能被 4 整除,但这并不意味 12 能被 8 整除。
  9. 假。见下文。

This last example raises a question: how might one decide whether $m \mid n$? Of course, if you had a trusted calculator, you could ask it for the value of $n \div m$. If it spits out anything other than an integer, you know $m \nmid n$. This seems a little like cheating though: we don't have division, so should we really use division to check divisibility?

最后一个例子引出一个问题:怎样判断 $m \mid n$ 是否成立?当然,若你有一台可靠的计数器,可以请它算出 $n \div m$ 的值。若结果不是整数,便知 $m \nmid n$。不过这有点像作弊:我们本没有除法,真的该用除法来检验整除性吗?

While we don't really know how to divide, we do know how to multiply. We might try multiplying $m$ by larger and larger numbers until we get close to $n$. How close? Well, we want to be sure that if we multiply $m$ by the next larger integer, we go over $n$.

虽然我们并不真正懂得如何做除法,但懂得如何做乘法。我们可以尝试把 $m$ 乘以越来越大的数,直到接近 $n$。要多接近?我们希望确定:当把 $m$ 乘以下一个更大的整数时,会超过 $n$。

For example, let's try this to decide whether $1642 \mid 136299$. Start finding multiples of 1642:

例如,用这一方法判断 $1642 \mid 136299$ 是否成立。先列出 1642 的倍数:
\begin{equation*} 1642 \cdot 2 = 3284 \qquad 1642 \cdot 3 = 4926 \qquad 1642\cdot 4 = 6568 \qquad \cdots \end{equation*}
(上式列出 1642 的前几个倍数。)

All of these are well less than 136299. I suppose we can jump ahead a bit:

这些都远小于 136299。我们不妨跳着往前算:
\begin{equation*} 1642 \cdot 50 = 82100 \qquad 1642 \cdot 80 = 131360 \qquad 1642 \cdot 85 = 139570 \end{equation*}
(上式为 1642 在 50、80、85 处的倍数。)

Ah, so we need to look somewhere between 80 and 85. Try 83:

看来答案在 80 与 85 之间。试 83:
\begin{equation*} 1642 \cdot 83 = 136286 \end{equation*}
(上式为 1642 的 83 倍。)

Is this the best we can do? How far are we from our desired 136299? If we subtract, we get $136299 - 136286 = 13$. So we know we cannot go up to 84, that will be too much. In other words, we have found that

这是最好的结果吗?离目标 136299 还差多少?相减得 $136299 - 136286 = 13$。可知不能进到 84,否则就超了。换言之,我们得到
\begin{equation*} 136299 = 83 \cdot 1642 + 13 \end{equation*}
(上式即带余除法表达式。)

Since $13 < 1642$, we can now safely say that $1642 \nmid 136299$.

由于 $13 < 1642$,现在我们可以断定 $1642 \nmid 136299$。

It turns out that the process we went through above can be repeated for any pair of numbers. We can always write the number $a$ as some multiple of the number $b$ plus some remainder. We know this because we know about division with remainder from elementary school. This is just a way of saying it using multiplication. Due to the procedural nature that can be used to find the remainder, this fact is usually called the division algorithm:

上述过程其实对任意两个数都能重复。我们总能把数 $a$ 写成数 $b$ 的某个倍数再加上某个余数。这一点我们都熟知,因为小学就学过带余除法。这只是用乘法语言重述同一件事。由于求余数有一套程序化的步骤,这一事实通常被称为除法算式(division algorithm)

The Division Algorithm

除法算式

Given any two integers $a$ and $b$, we can always find an integer $q$ such that

给定任意两个整数 $a$ 与 $b$,总能找到整数 $q$ 使得
\begin{equation*} a = qb + r \end{equation*}
(上式为带余除法的基本等式。)

where $r$ is an integer satisfying $0 \le r < |b|$.

其中 $r$ 是满足 $0 \le r < |b|$ 的整数。

The idea is that we can always take a large enough multiple of $b$ so that the remainder $r$ is as small as possible. We do allow the possibility of $r = 0$, in which case we have $b \mid a$.

其思想是:我们总可以取一个足够大的 $b$ 的倍数,使余数 $r$ 尽可能小。我们也允许 $r = 0$ 的情形,此时便有 $b \mid a$。

Remainder Classes 剩余类

The division algorithm tells us that there are only $b$ possible remainders when dividing by $b$. If we fix this divisor, we can group integers by the remainder. Each group is called a remainder class modulo $b$ (or sometimes residue class).

除法算式告诉我们:除以 $b$ 时只可能有 $b$ 种不同的余数。若固定这个除数,便可按余数把整数分组。每一组称为模 $b$ 的剩余类(有时也称残类)。

Example 2

示例 2

Describe the remainder classes modulo $5$.

描述模 $5$ 的各个剩余类。

Solution

解答

We want to classify numbers by what their remainder would be when divided by $5$. From the division algorithm, we know there will be exactly 5 remainder classes, because there are only 5 choices for what $r$ could be ($0 \le r < 5$).

我们要按「除以 5 的余数」来对数字分类。由除法算式,恰好有 5 个剩余类,因为 $r$ 只能取 5 种值($0 \le r < 5$)。

First consider $r = 0$. Here we are looking for all the numbers divisible by $5$ since $a = 5q+0$. In other words, the multiples of 5. We get the infinite set

先考虑 $r = 0$。此时我们寻找所有能被 5 整除的数,因为 $a = 5q+0$。也就是 5 的倍数,得到如下无限集合
\begin{equation*} \ldots, -15, -10, -5, 0, 5, 10, 15, 20, \ldots \end{equation*}
(上式为模 5 余 0 的剩余类。)

Notice we also include negative integers.

注意,其中也包含负整数。

Next consider $r = 1$. Which integers, when divided by 5, have remainder 1? Well, certainly 1, does, as does 6, and 11. Negatives? Here we must be careful: $-6$ does NOT have remainder 1. We can write $-6 = -2\cdot 5 + 4$ or $-6 = -1 \cdot 5 - 1$, but only one of these is a “correct” instance of the division algorithm: $r = 4$ since we need $r$ to be non-negative. So in fact, to get $r = 1$, we would have $-4$, or $-9$, etc. Thus we get the remainder class

再考虑 $r = 1$。哪些整数除以 5 余 1?显然 1、6、11 都如此。负数呢?这里必须小心:$-6$ 的余数并不是 1。我们可以写 $-6 = -2\cdot 5 + 4$ 或 $-6 = -1 \cdot 5 - 1$,但其中只有一个符合除法算式:因为要求 $r$ 非负,所以 $r = 4$。因此,要得到 $r = 1$,实际对应的是 $-4$、$-9$ 等。于是得到剩余类
\begin{equation*} \ldots, -14, -9, -4, 1, 6, 11, 16, 21, \ldots \end{equation*}
(上式为模 5 余 1 的剩余类。)

There are three more to go. The remainder classes for $2$, $3$, and $4$ are, respectively

还剩三个。模 5 余 2、余 3、余 4 的剩余类分别为
\begin{equation*} \ldots, -13, -8, -3, 2, 7, 12, 17, 22,\ldots \end{equation*} \begin{equation*} \ldots, -12, -7, -2, 3, 8, 13, 18, 23, \ldots \end{equation*} \begin{equation*} \ldots, -11, -6, -1, 4, 9, 14, 19, 24, \ldots\text{.} \end{equation*}
(以上三式依次为模 5 余 2、余 3、余 4 的剩余类。)

Note that in the example above, every integer is in exactly one remainder class. The technical way to say this is that the remainder classes modulo $b$ form a partition of the integers.1 It is possible to develop a mathematical theory of partitions, prove statements about all partitions in general and then apply those observations to our case here. The most important fact about partitions, is that it is possible to define an equivalence relation from a partition: this is a relationship between pairs of numbers which acts in all the important ways like the “equals” relationship.2 Again, there is a mathematical theory of equivalence relations which applies in many more instances than the one we look at here.

注意,在上例中,每个整数恰好属于一个剩余类。用术语说,就是模 $b$ 的剩余类构成了整数集的一个划分(partition)1 我们可以发展出一套划分的数学理论,先对一般意义上的所有划分证明命题,再把结论应用到这里。划分最重要的性质在于:可以从一个划分出发定义一种等价关系(equivalence relation),即数对之间的一种关系,它在所有重要方面都表现得如同「相等」关系。2 等价关系同样有一套数学理论,其适用范围远不止我们此处所讨论的情形。

All fun technical language aside, the idea is really simple. If two numbers belong to the same remainder class, then in some way, they are the same. That is, they are the same up to division by $b$. In the case where $b = 5$ above, the numbers $8$ and $23$, while not the same number, are the same when it comes to dividing by 5, because both have remainder $3$.

抛开这些有趣的专业术语,核心思想其实很简单:若两个数属于同一个剩余类,那么在某种意义上它们「相同」。也就是说,它们相对于「除以 $b$」是相同的。以上面 $b = 5$ 为例,8 和 23 虽不是同一个数,但在除以 5 时却是相同的,因为二者余数都是 3。

It matters what the divisor is: $8$ and $23$ are the same up to division by $5$, but not up to division by $7$, since $8$ has remainder of 1 when divided by 7 while 23 has a remainder of 2.

除数不同,结论也不同:8 和 23 相对于「除以 5」是相同的,但相对于「除以 7」却不同,因为 8 除以 7 余 1,而 23 除以 7 余 2。

With all this in mind, let's introduce some notation. We want to say that $8$ and 23 are basically the same, even though they are not equal. It would be wrong to say $8 = 23$. Instead, we write $8 \equiv 23$. But this is not always true. It works if we are thinking division by 5, so we need to denote that somehow. What we will actually write is this:

鉴于以上认识,我们引入一种记号。我们想表达 8 和 23 基本是相同的,尽管它们并不相等。说 $8 = 23$ 是错误的。于是我们改写作 $8 \equiv 23$。但这并非恒成立——它只在「除以 5」的意义下成立,所以我们需要把这一点标注出来。实际写法是:
\begin{equation*} 8 \equiv 23 \pmod{5} \end{equation*}
(上式表示 8 与 23 模 5 同余。)

which is read, “8 is congruent to 23 modulo 5” (or just “mod 5”). Of course then we could observe that

读作「8 与 23 模 5 同余」(或简作「模 5」)。自然我们也可以看到
\begin{equation*} 8 \not\equiv 23 \pmod{7} \end{equation*}
(上式表示 8 与 23 模 7 不同余。)

Congruence Modulo $n$

模 $n$ 同余

We say $a$ is congruent to $b$ modulo $n$, and write,

我们称 $a$ 与 $b$ 模 $n$ 同余,记作
\begin{equation*} a \equiv b \pmod{n} \end{equation*}
(上式即模 $n$ 同余的定义式。)

provided $a$ and $b$ have the same remainder when divided by $n$. In other words, provided $a$ and $b$ belong to the same remainder class modulo $n$.

前提是 $a$ 与 $b$ 除以 $n$ 时余数相同;换言之,$a$ 与 $b$ 属于模 $n$ 的同一个剩余类。

Many books define congruence modulo $n$ slightly differently. They say that $a \equiv b \pmod{n}$ if and only if $n \mid a-b$. In other words, two numbers are congruent modulo $n$, if their difference is a multiple of $n$. So which definition is correct? Turns out, it doesn't matter: they are equivalent.

许多教材对模 $n$ 同余的定义略有不同:它们说 $a \equiv b \pmod{n}$ 当且仅当 $n \mid a-b$。换言之,两个数模 $n$ 同余,当且仅当它们的差是 $n$ 的倍数。那么哪种定义才对?答案是:无所谓,二者等价。

To see why, consider two numbers $a$ and $b$ which are congruent modulo $n$. Then $a$ and $b$ have the same remainder when divided by $n$. We have

要理解原因,设 $a$ 与 $b$ 模 $n$ 同余。则 $a$ 与 $b$ 除以 $n$ 时余数相同。于是有
\begin{equation*} a = q_1 n + r \qquad\qquad b = q_2 n + r \end{equation*}
(上式中两式的余数 $r$ 相同。)

Here the two $r$'s really are the same. Consider what we get when we take the difference of $a$ and $b$:

这里两个 $r$ 确实相同。把 $a$ 与 $b$ 相减,得到
\begin{equation*} a-b = q_1n + r - (q_2n + r) = q_1n - q_2 n = (q_1-q_2)n \end{equation*}
(上式说明 $a-b$ 是 $n$ 的倍数。)

So $a-b$ is a multiple of $n$, or equivalently, $n \mid a-b$.

因此 $a-b$ 是 $n$ 的倍数,等价地,$n \mid a-b$。

On the other hand, if we assume first that $n \mid a-b$, so $a-b = kn$, then consider what happens if we divide each term by $n$. Dividing $a$ by $n$ will leave some remainder, as will dividing $b$ by $n$. However, dividing $kn$ by $n$ will leave 0 remainder. So the remainders on the left-hand side must cancel out. That is, the remainders must be the same.

反过来,若先假设 $n \mid a-b$,即 $a-b = kn$,再考虑各项除以 $n$ 的情形。$a$ 除以 $n$ 会留下某个余数,$b$ 除以 $n$ 也会留下某个余数。但 $kn$ 除以 $n$ 余数为 0。因此等式左边的余数必须相互抵消,也就是说,两边的余数必然相同。

Congruence and Divisibility

同余与整除

For any integers $a$, $b$, and $n$, we have

对任意整数 $a$、$b$、$n$,有
\begin{equation*} a \equiv b \pmod{n} \qquad \mbox{ if and only if } \qquad n \mid a-b. \end{equation*}
(上式给出同余与整除的等价关系。)

It will also be useful to switch back and forth between congruences and regular equations. The above fact helps with this. We know that $a \equiv b \pmod{n}$ if and only if $n \mid a-b$, if and only if $a-b = kn$ for some integer $k$. Rearranging that equation, we get $a = b + kn$. In other words, if $a$ and $b$ are congruent modulo $n$, then $a$ is $b$ more than some multiple of $n$. This conforms with our earlier observation that all the numbers in a particular remainder class are the same amount larger than the multiples of $n$.

在同余式与普通等式之间来回转换也很有用,上面的结论正可助我们做到这一点。我们知道:$a \equiv b \pmod{n}$ 当且仅当 $n \mid a-b$,当且仅当存在整数 $k$ 使 $a-b = kn$。整理该式得 $a = b + kn$。换言之,若 $a$ 与 $b$ 模 $n$ 同余,则 $a$ 比 $n$ 的某个倍数多出 $b$。这与我们先前的观察一致:同一剩余类中的各个数,都比 $n$ 的倍数多出相同的量。

Congruence and Equality

同余与相等

For any integers $a$, $b$, and $n$, we have

对任意整数 $a$、$b$、$n$,有
\begin{equation*} a \equiv b \pmod{n} \qquad \mbox{ if and only if } \qquad a = b + kn \mbox{ for some integer } k . \end{equation*}
(上式把同余改写为带余数的等式形式。)

Properties of Congruence 同余的性质

We said earlier that congruence modulo $n$ behaves, in many important ways, the same way equality does. Specifically, we could prove that congruence modulo $n$ is an equivalence relation, which would require checking the following three facts:

前面说过,模 $n$ 同余在许多重要方面表现得与相等相同。具体而言,我们可以证明模 $n$ 同余是一种等价关系,这只需验证以下三条性质:

Congruence Modulo $n$ is an Equivalence Relation

模 $n$ 同余是一种等价关系

Given any integers $a$, $b$, and $c$, and any positive integer $n$, the following hold:

对任意整数 $a$、$b$、$c$ 以及任意正整数 $n$,以下均成立:
  1. $a \equiv a \pmod{n}$.
  2. If $a \equiv b \pmod{n}$ then $b \equiv a \pmod{n}$.
  3. If $a \equiv b \pmod{n}$ and $b \equiv c \pmod{n}$, then $a \equiv c \pmod{n}$.
  1. $a \equiv a \pmod{n}$(自反性)。
  2. 若 $a \equiv b \pmod{n}$,则 $b \equiv a \pmod{n}$(对称性)。
  3. 若 $a \equiv b \pmod{n}$ 且 $b \equiv c \pmod{n}$,则 $a \equiv c \pmod{n}$(传递性)。

In other words, congruence modulo $n$ is reflexive, symmetric, and transitive, so is an equivalence relation.

换言之,模 $n$ 同余满足自反性、对称性与传递性,因而是一种等价关系。

You should take a minute to convince yourself that each of the properties above actually hold of congruence. Try explaining each using both the remainder and divisibility definitions.

你应该花点时间确认上述每条性质对同余确实成立。试着分别用「余数」与「整除」两种定义去解释每一条。

Next, consider how congruence behaves when doing basic arithmetic. We already know that if you subtract two congruent numbers, the result will be congruent to 0 (be a multiple of $n$). What if we add something congruent to 1 to something congruent to 2? Will we get something congruent to 3?

接下来看看同余在基本算术运算下的表现。我们已经知道:两个同余数相减,结果同余于 0(即 $n$ 的倍数)。如果给一个同余于 1 的数加上一个同余于 2 的数,会得到同余于 3 的数吗?

Congruence and Arithmetic

同余与算术运算

Suppose $a \equiv b \pmod{n}$ and $c \equiv d \pmod{n}$. Then the following hold:

设 $a \equiv b \pmod{n}$ 且 $c \equiv d \pmod{n}$。则以下均成立:
  1. $a+c \equiv b+d \pmod{n}$.
  2. $a-c \equiv b-d \pmod{n}$.
  3. $ac \equiv bd \pmod{n}$.
  1. $a+c \equiv b+d \pmod{n}$。
  2. $a-c \equiv b-d \pmod{n}$。
  3. $ac \equiv bd \pmod{n}$。

The above facts might be written a little strangely, but the idea is simple. If we have a true congruence, and we add the same thing to both sides, the result is still a true congruence. This sounds like we are saying:

上面的性质写法或许有点别扭,但意思很简单:若一个同余式成立,且我们往两边加上同一个量,所得同余式仍然成立。这听起来像是在说:

If $a \equiv b \pmod{n}$ then $a+c \equiv b+c \pmod{n}$.

若 $a \equiv b \pmod{n}$,则 $a+c \equiv b+c \pmod{n}$。

Of course this is true as well, it is the special case where $c = d$. But what we have works in more generality. Think of congruence as being “basically equal.” If we have two numbers which are basically equal, and we add basically the same thing to both sides, the result will be basically equal.

这当然也成立,它正是 $c = d$ 的特例。但我们所得结论更具一般性。不妨把同余理解为「基本相等」:若有两个基本相等的数,再往两边加上基本相同的量,结果仍基本相等。

This seems reasonable. Is it really true? Let's prove the first fact:

这听起来合理。真的成立吗?我们来证明第一条:

Proof

证明

Suppose $a \equiv b \pmod{n}$ and $c \equiv d \pmod{n}$. That means $a = b + kn$ and $c = d + jn$ for integers $k$ and $j$. Add these equations:

设 $a \equiv b \pmod{n}$ 且 $c \equiv d \pmod{n}$。则存在整数 $k$、$j$ 使 $a = b + kn$、$c = d + jn$。将两式相加:
\begin{equation*} a+c = b+d + kn + jn. \end{equation*}
(上式合并了两边的等式。)

But $kn + jn = (k+j)n$, which is just a multiple of $n$. So $a+c = b+d + (j+k)n$, or in other words, $a+c \equiv b+d \pmod{n}$

而 $kn + jn = (k+j)n$,只是 $n$ 的一个倍数。故 $a+c = b+d + (j+k)n$,换言之,$a+c \equiv b+d \pmod{n}$

$\square$

(证毕。)

The other two facts can be proved in a similar way.

另外两条性质可用类似方法证明。

One of the important consequences of these facts about congruences, is that we can basically replace any number in a congruence with any other number it is congruent to. Here are some examples to see how (and why) that works:

这些同余性质有一个重要推论:在同余式中,我们基本可以用任何与它同余的数替换其中的某个数。下面通过例子看看这如何(以及为何)可行:

Example 3

示例 3

Find the remainder of $3491$ divided by $9$.

求 $3491$ 除以 $9$ 的余数。

Solution

解答

We could do long division, but there is another way. We want to find $x$ such that $x \equiv 3491 \pmod{9}$. Now $3491 = 3000 + 400 + 90 + 1$. Of course $90 \equiv 0 \pmod 9$, so we can replace the 90 in the sum with 0. Why is this okay? We are actually subtracting the “same” thing from both sides:

当然可以做长除法,但还有别的方法。我们想找 $x$ 使 $x \equiv 3491 \pmod{9}$。而 $3491 = 3000 + 400 + 90 + 1$。显然 $90 \equiv 0 \pmod 9$,所以可把和式中的 90 换成 0。为何可以这样做?其实我们是在等式两边同时减去「相同」的量:
\begin{equation*} \begin{aligned} x &\equiv 3000 + 400 + 90 + 1 \pmod 9 \\ -0 &\equiv 90 \pmod 9 \\ x &\equiv 3000 + 400 + 0 + 1\pmod 9. \end{aligned} \end{equation*}
(上式将 $90 \equiv 0$ 从同余式中减去。)

Next, note that $400 = 4 \cdot 100$, and $100 \equiv 1 \pmod 9$ (since $9 \mid 99$). So we can in fact replace the 400 with simply a 4. Again, we are appealing to our claim that we can replace congruent elements, but we are really appealing to property 3 about the arithmetic of congruence: we know $100 \equiv 1 \pmod{9}$, so if we multiply both sides by $4$, we get $400 \equiv 4 \pmod 9$.

接着注意 $400 = 4 \cdot 100$,而 $100 \equiv 1 \pmod 9$(因为 $9 \mid 99$)。所以实际上可以把 400 直接换成 4。这同样是在利用「可替换同余元素」的主张,但本质上依赖的是同余算术的性质 3:已知 $100 \equiv 1 \pmod{9}$,两边同乘 4 即得 $400 \equiv 4 \pmod 9$。

Similarly, we can replace 3000 with 3, since $1000 = 1 + 999 \equiv 1 \pmod 9$. So our original congruence becomes

同理,$1000 = 1 + 999 \equiv 1 \pmod 9$,故可把 3000 换成 3。于是原同余式变为
\begin{equation*} x \equiv 3 + 4 + 0 + 1 \pmod 9 \end{equation*} \begin{equation*} x \equiv 8 \pmod 9. \end{equation*}
(合并后得 $x \equiv 8 \pmod 9$。)

Therefore $3491$ divided by 9 has remainder 8.

因此 $3491$ 除以 9 余 8。

The above example should convince you that the well known divisibility test for 9 is true: the sum of the digits of a number is divisible by 9 if and only if the original number is divisible by 9. In fact, we now know something more: any number is congruent to the sum of its digits, modulo 9.3 This works for 3 as well, but definitely not for any modulus in general.

上面的例子应能让你相信那条著名的「9 的整除判别法」是正确的:一个数的各位数字之和能被 9 整除,当且仅当原数能被 9 整除。事实上,我们现在还知道更多:任何数都与其各位数字之和模 9 同余。3 这一结论对 3 也成立,但一般而言对其他模数并不成立。

Example 4

示例 4

Find the remainder when $3^{123}$ is divided by 7.

求 $3^{123}$ 除以 7 的余数。

Solution

解答

Of course, we are working with congruence because we want to find the smallest positive $x$ such that $x \equiv 3^{123} \pmod 7$. Now first write $3^{123} = (3^3)^{41}$. We have:

我们当然要用同余,因为目标是找最小的正整数 $x$ 使 $x \equiv 3^{123} \pmod 7$。先把 $3^{123} = (3^3)^{41}$。于是:
\begin{equation*} 3^{123} = 27^{41} \equiv 6^{41} \pmod 7, \end{equation*}
(因为 $27 \equiv 6 \pmod 7$。)

since $27 \equiv 6 \pmod 7$. Notice further that $6^2 = 36$ is congruent to 1 modulo 7. Thus we can simplify further:

注意 $6^2 = 36$ 模 7 同余于 1,于是可进一步化简:
\begin{equation*} 6^{41} = 6\cdot (6^2)^{20} \equiv 6 \cdot 1^{20} \pmod 7. \end{equation*}
(上式把 $6^2$ 替换为 1。)

But $1^{20} = 1$, so we are done:

而 $1^{20} = 1$,于是得
\begin{equation*} 3^{123} \equiv 6 \pmod 7. \end{equation*}
(即 $3^{123}$ 除以 7 余 6。)

In the above example, we are using the fact that if $a \equiv b \pmod n$, then $a^p \equiv b^p \pmod n$. This is just applying property 3 a bunch of times.

在上例中,我们用到了这样一个事实:若 $a \equiv b \pmod n$,则 $a^p \equiv b^p \pmod n$。这不过是多次运用性质 3。

So far we have seen how to add, subtract and multiply with congruences. What about division? There is a reason we have waited to discuss it. It turns out that we cannot simply divide. In other words, even if $ad \equiv bd \pmod n$, we do not know that $a \equiv b \pmod n$. Consider, for example:

至此我们已见过同余的加、减、乘运算。那除法呢?我们留到现在才讨论是有原因的:同余并不能直接相除。也就是说,即便 $ad \equiv bd \pmod n$,也不能推出 $a \equiv b \pmod n$。例如:
\begin{equation*} 18 \equiv 42 \pmod 8. \end{equation*}
(上式成立。)

This is true. Now $18$ and $42$ are both divisible by 6. However,

这确实成立。而 18 与 42 都能被 6 整除。然而,
\begin{equation*} 3 \not\equiv 7 \pmod 8. \end{equation*}
(上式说明约去 6 后同余不再成立。)

While this doesn't work, note that $3 \equiv 7 \pmod 4$. We cannot divide $8$ by 6, but we can divide 8 by the greatest common factor of $8$ and $6$. Will this always happen?

虽然这样不行,但请注意 $3 \equiv 7 \pmod 4$。我们不能拿 8 除以 6,却可以用 8 和 6 的最大公因数去除 8。这会是普遍规律吗?

Suppose $ad \equiv bd \pmod n$. In other words, we have $ad = bd + kn$ for some integer $k$. Of course $ad$ is divisible by $d$, as is $bd$. So $kn$ must also be divisible by $d$. Now if $n$ and $d$ have no common factors (other than 1), then we must have $d \mid k$. But in general, if we try to divide $kn$ by $d$, we don't know that we will get an integer multiple of $n$. Some of the $n$ might get divided as well. To be safe, let's divide as much of $n$ as we can. Take the largest factor of both $d$ and $n$, and cancel that out from $n$. The rest of the factors of $d$ will come from $k$, no problem.

设 $ad \equiv bd \pmod n$,即存在整数 $k$ 使 $ad = bd + kn$。显然 $ad$ 能被 $d$ 整除,$bd$ 也能。因此 $kn$ 也必须能被 $d$ 整除。若 $n$ 与 $d$ 没有公共因数(除 1 外),则必有 $d \mid k$。但一般地,若我们尝试用 $d$ 去除 $kn$,并不保证得到 $n$ 的整数倍——$n$ 的一部分也可能被除掉。为稳妥起见,我们尽可能多地从 $n$ 中约去:取 $d$ 与 $n$ 的最大公因数,把它从 $n$ 中消去。$d$ 的其余因数则由 $k$ 提供,不成问题。

We will call the largest factor of both $d$ and $n$ the $\gcd(d,n)$, for greatest common divisor. In our example above, $\gcd(6,8) = 2$ since the greatest divisor common to 6 and 8 is 2.

我们把 $d$ 与 $n$ 的最大公因数记作 $\gcd(d,n)$,即 最大公约数(greatest common divisor)。在前面的例子中,$\gcd(6,8) = 2$,因为 6 和 8 的公约数中最大的是 2。

Congruence and Division

同余与除法

Suppose $ad \equiv bd \pmod n$. Then $a \equiv b \pmod{\frac{n}{\gcd(d,n)}}$.

设 $ad \equiv bd \pmod n$。则 $a \equiv b \pmod{\frac{n}{\gcd(d,n)}}$。

If $d$ and $n$ have no common factors then $\gcd(d,n) = 1$, so $a \equiv b \pmod n$.

若 $d$ 与 $n$ 互素,则 $\gcd(d,n) = 1$,于是 $a \equiv b \pmod n$。

Example 5

示例 5

Simplify the following congruences using division: (a) $24 \equiv 39 \pmod 5$ and (b) $24 \equiv 39 \pmod{15}$.

用除法化简下列同余式:(a) $24 \equiv 39 \pmod 5$,(b) $24 \equiv 39 \pmod{15}$。

Solution

解答

$a$ Both $24$ and $39$ are divisible by $3$, and $3$ and $5$ have no common factors, so we get

$a$ 24 与 39 都能被 3 整除,且 3 与 5 没有公共因数,于是得到
\begin{equation*} 8 \equiv 13 \pmod 5. \end{equation*}
(上式约去公因数 3。)

$b$ Again, we can divide by 3. However, doing so blindly gives us $8 \equiv 13 \pmod{15}$ which is no longer true. Instead, we must also divide the modulus 15 by the greatest common factor of $3$ and $15$, which is $3$. Again we get

$b$ 同样可以约去 3。但若盲目约去,会得到 $8 \equiv 13 \pmod{15}$,这已不再成立。正确的做法是:同时用 3 和 15 的最大公因数 3 去除模数 15。于是仍得到
\begin{equation*} 8 \equiv 13 \pmod 5. \end{equation*}
(上式约去 3 并把模数也除以 3。)

Solving Congruences 同余方程求解

Now that we have some algebraic rules to govern congruence relations, we can attempt to solve for an unknown in a congruence. For example, is there a value of $x$ that satisfies,

既然已经有了约束同余关系的代数规则,我们就可以尝试求解同余式中的未知量。例如,是否存在 $x$ 满足
\begin{equation*} 3x + 2 \equiv 4 \pmod{5}, \end{equation*}
(上式为一个一次同余方程。)

and if so, what is it?

若存在,$x$ 是什么?

In this example, since the modulus is small, we could simply try every possible value for $x$. There are really only 5 to consider, since any integer that satisfied the congruence could be replaced with any other integer it was congruent to modulo 5. Here, when $x = 4$ we get $3x + 2 = 14$ which is indeed congruent to 4 modulo 5. This means that $x = 9$ and $x = 14$ and $x = 19$ and so on will each also be a solution because as we saw above, replacing any number in a congruence with a congruent number does not change the truth of the congruence.

此例中模数很小,我们大可把 $x$ 的可能取值逐一试遍。实际上只需考虑 5 个值,因为任何满足同余式的整数都可以换成与它模 5 同余的另一个整数。这里当 $x = 4$ 时,$3x + 2 = 14$,确实模 5 同余于 4。这意味着 $x = 9$、$x = 14$、$x = 19$ 等等也都是解,因为如前所见,把同余式中的某个数换成同余的数,并不改变同余式的真假。

So in this example, simply compute $3x + 2$ for values of $x \in \{0,1,2,3,4\}$. This gives 2, 5, 8, 11, and 14 respectively, for which only 14 is congruent to 4.

因此在本例中,只需对 $x \in \{0,1,2,3,4\}$ 计算 $3x + 2$,分别得到 2、5、8、11、14,其中只有 14 同余于 4。

Let's also see how you could solve this using our rules for the algebra of congruences. Such an approach would be much simpler than the trial and error tactic if the modulus was larger. First, we know we can subtract 2 from both sides:

我们再看如何用同余代数规则来求解。若模数较大,这种方法比穷举简便得多。首先,两边同减 2:
\begin{equation*} 3x \equiv 2 \pmod{5}. \end{equation*}
(上式由原方程移项得到。)

Then to divide both sides by 3, we first add 0 to both sides. Of course, on the right-hand side, we want that 0 to be a 10 (yes, $10$ really is 0 since they are congruent modulo 5). This gives,

接着要把两边同除以 3,我们先往两边加上 0。当然,在右边我们希望这个 0 实际上是 10(没错,$10$ 确实等于 0,因为它们模 5 同余)。于是得到
\begin{equation*} 3x \equiv 12 \pmod{5}. \end{equation*}
(上式右边加上了 $10 \equiv 0$。)

Now divide both sides by 3. Since $\gcd(3,5) = 1$, we do not need to change the modulus:

现在两边同除以 3。由于 $\gcd(3,5) = 1$,无需改变模数:
\begin{equation*} x \equiv 4 \pmod{5}. \end{equation*}
(上式即通解形式。)

Notice that this in fact gives the general solution: not only can $x = 4$, but $x$ can be any number which is congruent to 4. We can leave it like this, or write “$x = 4 + 5k$ for any integer $k$.”

注意,这其实给出的就是通解:$x$ 不仅可以是 4,也可以是任何与 4 同余的数。我们可以保留这种写法,也可以写成「对任意整数 $k$,$x = 4 + 5k$」。

Example 6

示例 6

Solve the following congruences for $x$.

求解下列关于 $x$ 的同余方程。
  1. $7x \equiv 12 \pmod{13}$.
  2. $84x - 38 \equiv 79 \pmod{15}$.
  3. $20x \equiv 23 \pmod{14}$.
  1. $7x \equiv 12 \pmod{13}$。
  2. $84x - 38 \equiv 79 \pmod{15}$。
  3. $20x \equiv 23 \pmod{14}$。

Solution

解答
  1. All we need to do here is divide both sides by 7. We add 13 to the right-hand side repeatedly until we get a multiple of 7 (adding 13 is the same as adding 0, so this is legal). We get $25$, $38$, $51$, $64$, $77$ – got it. So we have: \begin{equation*} \begin{aligned} 7x &\equiv 12 \pmod{13} \\ 7x &\equiv 77 \pmod{13} \\ x &\equiv 11 \pmod{13}. \end{aligned} \end{equation*}
  2. Here, since we have numbers larger than the modulus, we can reduce them prior to applying any algebra. We have $84 \equiv 9$, $38 \equiv 8$ and $79 \equiv 4$. Thus, \begin{equation*} \begin{aligned} 84x - 38 &\equiv 79 \pmod{15} \\ 9x - 8 &\equiv 4 \pmod{15} \\ 9x &\equiv 12 \pmod{15} \\ 9x &\equiv 72 \pmod{15}. \end{aligned} \end{equation*} We got the 72 by adding $0 \equiv 60 \pmod{15}$ to both sides of the congruence. Now divide both sides by 9. However, since $\gcd(9, 15) = 3$, we must divide the modulus by 3 as well: \begin{equation*} x \equiv 8 \pmod 5. \end{equation*} So the solutions are those values which are congruent to 8, or equivalently 3, modulo 5. This means that in some sense there are 3 solutions modulo 15: 3, 8, and 13. We can write the solution: \begin{equation*} x \equiv 3 \pmod{15}; \quad x \equiv 8 \pmod{15}; \quad x \equiv 13 \pmod{15}. \end{equation*}
  3. First, reduce modulo 14: \begin{equation*} 20x \equiv 23 \pmod{14} \end{equation*} \begin{equation*} 6x \equiv 9 \pmod{14}. \end{equation*} We could now divide both sides by 3, or try to increase 9 by a multiple of 14 to get a multiple of 6. If we divide by 3, we get, \begin{equation*} 2x \equiv 3 \pmod{14}. \end{equation*} Now try adding multiples of 14 to 3, in hopes of getting a number we can divide by 2. This will not work! Every time we add 14 to the right side, the result will still be odd. We will never get an even number, so we will never be able to divide by 2. Thus there are no solutions to the congruence.
  1. 这里只需把两边同除以 7。我们不断往右边加 13,直到成为 7 的倍数(加 13 等同于加 0,故合法)。依次得到 $25$、$38$、$51$、$64$、$77$——找到了。于是有: \begin{equation*} \begin{aligned} 7x &\equiv 12 \pmod{13} \\ 7x &\equiv 77 \pmod{13} \\ x &\equiv 11 \pmod{13}. \end{aligned} \end{equation*}
  2. 这里由于出现比模数大的数,可先化简再运算。我们有 $84 \equiv 9$、$38 \equiv 8$、$79 \equiv 4$。于是: \begin{equation*} \begin{aligned} 84x - 38 &\equiv 79 \pmod{15} \\ 9x - 8 &\equiv 4 \pmod{15} \\ 9x &\equiv 12 \pmod{15} \\ 9x &\equiv 72 \pmod{15}. \end{aligned} \end{equation*} 其中 72 是由同余式两边同加 $0 \equiv 60 \pmod{15}$ 得到的。现在两边同除以 9。但注意 $\gcd(9, 15) = 3$,所以模数也必须除以 3: \begin{equation*} x \equiv 8 \pmod 5. \end{equation*} 因此解是那些模 5 同余于 8(等价地,同余于 3)的值。这意味着从某种意义上看,模 15 下共有 3 个解:3、8、13。可写作: \begin{equation*} x \equiv 3 \pmod{15}; \quad x \equiv 8 \pmod{15}; \quad x \equiv 13 \pmod{15}. \end{equation*}
  3. 先在模 14 下化简: \begin{equation*} 20x \equiv 23 \pmod{14} \end{equation*} \begin{equation*} 6x \equiv 9 \pmod{14}. \end{equation*} 此时可两边同除以 3,也可尝试把 9 加上 14 的某个倍数以凑成 6 的倍数。若除以 3,得 \begin{equation*} 2x \equiv 3 \pmod{14}. \end{equation*} 再试着往右边的 3 加上 14 的倍数,期望得到一个能被 2 整除的数。这行不通!每往右边加一次 14,结果仍是奇数。我们永远得不到偶数,也就永远无法除以 2。因此该同余方程无解。

The last congruence above illustrates the way in which congruences might not have solutions. We could have seen this immediately in fact. Look at the original congruence:

上面最后一个同余方程说明了同余方程可能无解的情形。其实我们本可以立刻看出来。看原方程:
\begin{equation*} 20x \equiv 23 \pmod{14}. \end{equation*}
(上式即待解同余方程。)

If we write this as an equation, we get

若把它写成等式,得到
\begin{equation*} 20x = 23 + 14k, \end{equation*}
(上式为对应的丢番图等式。)

or equivalently $20x - 14k = 23$. We can easily see there will be no solution to this equation in integers. The left-hand side will always be even, but the right-hand side is odd. A similar problem would occur if the right-hand side was divisible by any number the left-hand side was not.

等价地,$20x - 14k = 23$。容易看出该方程在整数范围内无解:左边恒为偶数,右边却是奇数。若右边能被左边所不能被某个数整除,也会出现类似问题。

So in general, given the congruence

因此一般而言,对于同余式
\begin{equation*} ax \equiv b \pmod{n}, \end{equation*}
(上式为一般一次同余方程。)

if $a$ and $n$ are divisible by a number which $b$ is not divisible by, then there will be no solutions. In fact, we really only need to check one divisor of $a$ and $n$: the greatest common divisor. Thus, a more compact way to say this is:

若 $a$ 与 $n$ 都能被某个数整除,而 $b$ 不能,则无解。事实上,我们只需检查 $a$ 与 $n$ 的一个公因数,即最大公约数。因此可以更紧凑地表述为:

Congruences with no solutions

无解的同余方程

If $\gcd(a,n) \nmid b$, then $ax \equiv b \pmod{n}$ has no solutions.

若 $\gcd(a,n) \nmid b$,则 $ax \equiv b \pmod{n}$ 无解。

Solving Linear Diophantine Equations 线性丢番图方程求解

Discrete math deals with whole numbers of things. So when we want to solve equations, we usually are looking for integer solutions. Equations which are intended to only have integer solutions were first studied by in the third century by the Greek mathematician Diophantus of Alexandria, and as such are called Diophantine equations. Probably the most famous example of a Diophantine equation is $a^2 + b^2 = c^2$. The integer solutions to this equation are called Pythagorean triples. In general, solving Diophantine equations is hard (in fact, there is provably no general algorithm for deciding whether a Diophantine equation has a solution, a result known as Matiyasevich's Theorem). We will restrict our focus to linear Diophantine equations, which are considerably easier to work with.

离散数学处理的是整数个的对象。因此解方程时,我们通常寻找整数解。那些只要求整数解的方程,最早由公元三世纪亚历山大城的希腊数学家丢番图研究,因而被称为丢番图方程。最著名的例子大概是 $a^2 + b^2 = c^2$,它的整数解称为勾股三元组。一般而言,求解丢番图方程很难(事实上,已证明不存在判断丢番图方程是否有解的一般算法,这一结论即马蒂亚塞维奇定理)。我们这里只关注线性丢番图方程,它要容易处理得多。

Diophantine Equations

丢番图方程

An equation in two or more variables is called a Diophantine equation if only integers solutions are of interest. A linear Diophantine equation takes the form $a_1x_1 + a_2x_2 + \cdots + a_nx_n = b$ for constants $a_1,\ldots, a_n, b$.

若只关心整数解,则含两个或更多变量的方程称为丢番图方程。一个线性丢番图方程具有形式 $a_1x_1 + a_2x_2 + \cdots + a_nx_n = b$,其中 $a_1,\ldots, a_n, b$ 为常数。

A solution to a Diophantine equation is a solution to the equation consisting only of integers.

丢番图方程的,是指仅由整数构成的方程解。

We have the tools we need to solve linear Diophantine equations. We will consider, as a main example, the equation

求解线性丢番图方程所需的工具我们都已具备。作为主例,考虑方程
\begin{equation*} 51x + 87y = 123. \end{equation*}
(上式为一个二元线性丢番图方程。)

The general strategy will be to convert the equation to a congruence, then solve that congruence.4 This is certainly not the only way to proceed. A more common technique would be to apply the Euclidean algorithm. Our way can be a little faster, and is presented here primarily for variety. Let's work this particular example to see how this might go.

一般策略是先把方程转化为同余式,再解该同余式。4 这当然不是唯一的做法。更常见的技巧是运用欧几里得算法。我们的方法有时更快一些,此处介绍主要是为了提供另一种思路。我们具体演算这个例子,看看过程如何。

First, check if perhaps there are no solutions because a divisor of $51$ and $87$ is not a divisor of $123$. Really, we just need to check whether $\gcd(51, 87) \mid 123$. This greatest common divisor is 3, and yes $3 \mid 123$. At this point, we might as well factor out this greatest common divisor. So instead, we will solve:

首先,检查是否可能因 51 与 87 的某个公因数不能整除 123 而无解。其实只需判断 $\gcd(51, 87) \mid 123$ 是否成立。最大公约数是 3,且确实有 $3 \mid 123$。此时不妨先提取这个最大公约数。于是我们转而求解:
\begin{equation*} 17x + 29y = 41. \end{equation*}
(上式为约去公因数 3 后的方程。)

Now observe that if there are going to be solutions, then for those values of $x$ and $y$, the two sides of the equation must have the same remainder as each other, no matter what we divide by. In particular, if we divide both sides by 17, we must get the same remainder. Thus we can safely write

现在注意:若有解,那么对相应的 $x$、$y$,方程两边无论除以什么数都必须余数相同。特别地,若两边同除以 17,余数必相同。因此我们可以放心地写出
\begin{equation*} 17x + 29y \equiv 41 \pmod{17}. \end{equation*}
(上式由原方程两边模 17 得到。)

We choose 17 because $17x$ will have remainder 0. This will allow us to reduce the congruence to just one variable. We could have also moved to a congruence modulo 29, although there is usually a good reason to select the smaller choice, as this will allow us to reduce the other coefficient. In our case, we reduce the congruence as follows:

我们选择 17,是因为 $17x$ 的余数为 0,从而可把同余式化为只含一个变量。当然也可以转到模 29 的同余,但通常有充分理由选较小的数,这样便于消去另一个系数。本例中化简如下:
\begin{equation*} \begin{aligned} 17x + 29y &\equiv 41 \pmod{17} \\ 0x + 12y &\equiv 7 \pmod{17} \\ 12 y &\equiv 24 \pmod{17} \\ y &\equiv 2 \pmod{17}. \end{aligned} \end{equation*}
(上式逐次化简,最终得 $y \equiv 2 \pmod{17}$。)

Now at this point we know $y = 2 + 17k$ will work for any integer $k$. If we haven't made a mistake, we should be able to plug this back into our original Diophantine equation to find $x$:

至此可知,对任意整数 $k$,$y = 2 + 17k$ 都成立。若前面没有算错,把它代回原丢番图方程应能求出 $x$:
\begin{equation*} \begin{aligned} 17x + 29(2 + 17k) &= 41\\ 17x &= -17 - 29\cdot 17k\\ x &= -1-29k. \end{aligned} \end{equation*}
(上式回代并解出 $x = -1-29k$。)

We have now found all solutions to the Diophantine equation. For each $k$, $x = -1-29k$ and $y = 2 + 17k$ will satisfy the equation. We could check this for a few cases. If $k = 0$, the solution is $(-1,2)$, and yes, $-17 + 2\cdot 29 = 41$. If $k = 3$, the solution is $(-88, 53)$. If $k = -2$, we get $(57, -32)$.

到此我们已求得该丢番图方程的全部解。对每个 $k$,$x = -1-29k$、$y = 2 + 17k$ 都满足方程。可任取几组验证:若 $k = 0$,解为 $(-1,2)$,确有 $-17 + 2\cdot 29 = 41$;若 $k = 3$,解为 $(-88, 53)$;若 $k = -2$,得 $(57, -32)$。

To summarize this process, to solve $ax + by = c$, we,

总结上述过程,求解 $ax + by = c$ 的步骤如下:
  1. Divide both sides of the equation by $\gcd(a,b)$ (if this does not leave the right-hand side as an integer, there are no solutions). Let's assume that $ax + by = c$ has already been reduced in this way.
  2. Pick the smaller of $a$ and $b$ (here, assume it is $b$), and convert to a congruence modulo $b$: \begin{equation*} ax + by \equiv c \pmod{b}. \end{equation*} This will reduce to a congruence with one variable, $x$: \begin{equation*} ax \equiv c \pmod{b}. \end{equation*}
  3. Solve the congruence as we did in the previous section. Write your solution as an equation, such as, \begin{equation*} x = n + kb \end{equation*}
  4. Plug this into the original Diophantine equation, and solve for $y$.
  5. If we want to know solutions in a particular range (for example, $0 \le x, y \le 20$), pick different values of $k$ until you have all required solutions.
  1. 方程两边同除以 $\gcd(a,b)$(若右边结果不是整数,则无解)。以下设 $ax + by = c$ 已完成这一步化简。
  2. 取 $a$、$b$ 中较小者(此处设为 $b$),转化为模 $b$ 的同余式: \begin{equation*} ax + by \equiv c \pmod{b}. \end{equation*} 这将化为只含一个变量 $x$ 的同余式: \begin{equation*} ax \equiv c \pmod{b}. \end{equation*}
  3. 按前一节的方法解该同余式,并把解写成等式形式,例如 \begin{equation*} x = n + kb \end{equation*}
  4. 把它代回原丢番图方程,解出 $y$。
  5. 若想求某个范围内的解(例如 $0 \le x, y \le 20$),则取不同的 $k$ 值,直到得到所有需要的解。

Here is another example:

再看一例:

Example 7

示例 7

How can you make \$6.37 using just 5-cent and 8-cent stamps? What is the smallest and largest number of stamps you could use?

只用 5 分和 8 分邮票,如何凑出 \$6.37?所用邮票最少和最多分别是多少张?

Solution

解答

First, we need a Diophantine equation. We will work in numbers of cents. Let $x$ be the number of 5-cent stamps, and $y$ be the number of 8-cent stamps. We have:

首先需要建立一个丢番图方程。我们以「分」为单位计算。设 $x$ 为 5 分邮票的张数,$y$ 为 8 分邮票的张数。于是:
\begin{equation*} 5x + 8y = 637. \end{equation*}
(上式表示总面额 637 分。)

Convert to a congruence and solve:

转化为同余式并求解:
\begin{equation*} \begin{aligned} 8y &\equiv 367 \pmod{5}\\ 3y &\equiv 2 \pmod 5\\ 3y &\equiv 12 \pmod 5\\ y &\equiv 4 \pmod 5. \end{aligned} \end{equation*}
(上式逐步化简,得 $y \equiv 4 \pmod 5$。)

Thus $y = 4 + 5k$. Then $5x + 8(4+5k) = 637$, so $x = 121 - 8k$.

因此 $y = 4 + 5k$。代入 $5x + 8(4+5k) = 637$,得 $x = 121 - 8k$。

This says that one way to make \$6.37 is to take 121 of the 5-cent stamps and 4 of the 8-cent stamps. To find the smallest and largest number of stamps, try different values of $k$.

这说明一种凑法是取 121 张 5 分邮票和 4 张 8 分邮票。要找出最少与最多的邮票张数,可尝试不同的 $k$ 值。
$k$$(x,y)$Stamps
-1$(129, -1)$not possible
0$(121, 4)$125
1$(113, 9)$122
2$(105, 13)$119
$\vdots$$\vdots$$\vdots$
(上表列出线性丢番图方程 $5x+8y=637$ 的部分整数解。参数 $k$ 取不同整数值对应一组邮票数量 $(x,y)$(分别为 5 分与 8 分邮票张数);「Stamps」列为所用邮票总张数 $x+y$。当 $k=-1$ 时 $y=-1$ 不合题意(张数不能为负),故标为 not possible。随 $k$ 增大,5 分邮票减少、8 分邮票增多,总张数相应变化。)

This is no surprise. Having the most stamps means we have as many 5-cent stamps as possible, and to get the smallest number of stamps would require have the least number of 5-cent stamps. To minimize the number of 5-cent stamps, we want to pick $k$ so that $121-8k$ is as small as possible (but still positive). When $k = 15$, we have $x = 1$ and $y = 79$.

这并不意外。邮票总张数最多,意味着 5 分邮票尽可能多;要使总张数最少,则 5 分邮票应尽可能少。为了让 $121-8k$ 尽量小(但仍为正),取 $k = 15$,此时 $x = 1$、$y = 79$。

Therefore, to make \$6.37, you can us as few as 80 stamps (1 5-cent stamp and 79 8-cent stamps) or as many as 125 stamps (121 5-cent stamps and 4 8-cent stamps).

因此,凑出 \$6.37 最少可用 80 张邮票(1 张 5 分、79 张 8 分),最多可用 125 张(121 张 5 分、4 张 8 分)。

Using this method, as long as you can solve linear congruences in one variable, you can solve linear Diophantine equations of two variables. There are times though that solving the linear congruence is a lot of work. For example, suppose you need to solve,

用这种方法,只要你会解一元线性同余式,就能解二元线性丢番图方程。不过有时解那个线性同余式相当费劲。例如,假设你要解
\begin{equation*} 13x \equiv 6 \pmod{51}. \end{equation*}
(上式是一个一元线性同余式。)

You could keep adding 51 to the right side until you get a multiple of 13: You would get 57, 108, 159, 210, 261, 312, and 312 is the first of these that is divisible by 13. This works, but is really too much work. Instead we could convert back to a Diophantine equation:

你当然可以不断往右边加 51,直到成为 13 的倍数:会依次得到 57、108、159、210、261、312,其中 312 是第一个能被 13 整除的。这可行,但实在太费功夫。不如我们反向转回丢番图方程:
\begin{equation*} 13x = 6 + 51k \end{equation*}
(上式即对应的丢番图等式。)

Now solve this like we have in this section. Write it as a congruence modulo 13:

现在按本节的方法解它。把它写成模 13 的同余式:
\begin{equation*} \begin{aligned} 0 &\equiv 6 + 51k \pmod{13}\\ -12k &\equiv 6 \pmod{13}\\ 2k &\equiv -1 \pmod{13}\\ 2k &\equiv 12 \pmod{13}\\ k &\equiv 6 \pmod{13} \end{aligned} \end{equation*}
(上式逐步化简,得 $k \equiv 6 \pmod{13}$。)

so $k = 6 + 13j$. Now go back and figure out $x$:

因此 $k = 6 + 13j$。再回代求出 $x$:
\begin{equation*} \begin{aligned} 13x &= 6 + 51(6+13j)\\ x &= 24 + 51j. \end{aligned} \end{equation*}
(上式回代得 $x = 24 + 51j$。)

Of course you could do this switching back and forth between congruences and Diophantine equations as many times as you like. If you only used this technique, you would essentially replicate the Euclidean algorithm, a more standard way to solve Diophantine equations.

自然,你可以在同余式与丢番图方程之间反复来回转换任意多次。如果只用这一技巧,本质上就是在重演欧几里得算法——一种更标准的丢番图方程求解方法。