Chap4 Number Theory
约 954 个字 34 行代码 预计阅读时间 6 分钟
1. Divisibility and Modular Arithmetic
Number theory studies arithmetic properties of integers. In computer science, it appears in hashing, congruence calculations, digital signatures, and cryptography.
1.1 Divisibility
Let \(a,b\in\mathbb{Z}\) and \(a\neq0\).
Divides:
If \(a\mid b\), then \(a\) is a factor or divisor of \(b\), and \(b\) is a multiple of \(a\).
Basic Properties
For \(a,b,c\in\mathbb{Z}\) and \(a\neq0\):
- \(a\mid0\)
- \((a\mid b\wedge a\mid c)\Rightarrow a\mid(b+c)\)
- \(a\mid b\Rightarrow a\mid bc\)
- \((a\mid b\wedge b\mid c)\Rightarrow a\mid c\)
More generally, if \(a\mid b\) and \(a\mid c\), then \(a\mid(mb+nc)\) for all \(m,n\in\mathbb{Z}\).
1.2 Division Algorithm
For any integers \(a,d\) with \(d\neq0\), there exist unique integers \(q,r\) such that
\(q\) is the quotient and \(r\) is the remainder.
We write:
The remainder is always nonnegative in this theorem, for example: + \(-11 \operatorname{ div } 3=-4\) + \(-11 \bmod3 = 1\)
1.3 Modular and Congruence
Let \(a,b\in\mathbb{Z}\) and \(m\in\mathbb{Z}^+\).
Congruence modulo(同余) \(m\):
Equivalent forms:
Every integer is congruent to its remainder:
Congruence Rules
If
then
and
Therefore:
2. Integer Representations and Algorithms
2.1 Base-\(b\) Representation
For any positive integers \(n\) and \(b>1\), there is a unique sequence of digits
where \(0\leq a_i<b\), such that
We write this as
Common bases:
- base \(10\): decimal.
- base \(2\): binary.
- base \(8\): octal.
- base \(16\): hexadecimal.
To convert \(n\) to base \(b\), repeatedly divide by \(b\) and record remainders from right to left.
1 2 3 4 5 6 7 8 | |
2.2 Integer Operations
Binary addition, multiplication, and division are built from the same ideas as decimal arithmetic.
Binary addition uses a carry bit:
1 2 3 4 5 6 7 8 | |
Binary multiplication adds shifted copies of one factor:
1 2 3 4 5 | |
2.3 Modular Exponentiation
The modular exponentiation problem is to compute \(b^n\bmod m\) efficiently.
We use Exponentiation by squaring:
1 2 3 4 5 6 7 | |
RSA Encryption
RSA is an application of modular exponentiation and modular inverses.
- Choose two large primes \(P,Q\).
- Let \(N=PQ\) and \(L=(P-1)(Q-1)\).
- Choose \(E<L\) with \(\gcd(E,L)=1\).
- Find \(D\) such that
$$ DE\equiv1\pmod L $$
The public key is \((E,N)\) and the private key is \(D\).
Encryption:
Decryption:
This is treated as an application rather than a core number-theory theorem here.
3. Primes and GCD
3.1 Prime Numbers
An integer \(p>1\) is prime(质数) if its only positive divisors are \(1\) and \(p\).
An integer greater than \(1\) that is not prime is composite(合数).
Thm. Fundamental Theorem of Arithmetic: Every positive integer has a unique prime factorization when primes are written in nondecreasing order.
- \(2000=2^4\cdot5^3\)
- \(2001=3\cdot23\cdot29\)
- \(2002=2\cdot7\cdot11\cdot13\)
Thm. Trial Division: If \(n\) is composite, then \(n\) has a prime divisor less than or equal to \(\sqrt n\).
To test whether \(n\) is prime, it is enough to check divisibility by primes \(\leq\sqrt n\).
Show 101 Is Prime
Since
it is enough to test primes \(2,3,5,7\).
\(101\) is divisible by none of them, so \(101\) is prime.
Thm. There are infinitely many primes. See proof in Chap1 6.3.1.
Mersenne Primes
Prime numbers of the form
where \(p\) is prime are called Mersenne primes.
Examples:
But \(2^{11}-1=2047=23\cdot89\), so not every number of this form is prime.
3.2 GCD & LCM
For integers \(a,b\) not both \(0\), the greatest common divisor is
and the least common multiple is
If \(a=p_1^{a_1}p_2^{a_2}\cdots p_n^{a_n}\) and $ b=p_1^{b_1}p_2^{b_2}\cdots p_n^{b_n}$, then
For positive integers \(a,b\):
3.3 Relative Primality
Integers \(a\) and \(b\) are relatively prime or coprime if
A set of integers is pairwise relatively prime if every pair of distinct elements is relatively prime. For example \(\{10,17,21\}\).
3.4 Euclidean Algorithm
1 2 3 4 5 6 | |
The last nonzero remainder is the gcd.
Example
3.5 Bézout's Theorem
If \(a\) and \(b\) are positive integers, then there exist integers \(s,t\) such that
\(s\) and \(t\) are called Bézout coefficients(贝祖系数), the equation is called Bézout's identity.
Back Substitution
Express \(\gcd(252,198)=18\) as a linear combination.
Euclidean algorithm:
Work backward: each time, represent the smaller number as a combination of the larger number in Euclidean algorithm
Lem. If \(\gcd(a,b)=1\) and \(a\mid bc\), then \(a\mid c\).
Lem. If \(ac\equiv bc\pmod m\), and \(\gcd(c,m)=1\), then \(a\equiv b\pmod m\).
Lem. If \(p\) is prime and \(p\mid a_1a_2\cdots a_n\), then \(p\mid a_i\) for some \(i\).
4. Solving Congruences
4.1 Inverses Modulo \(m\)
An integer \(\overline{a}\) is an inverse of \(a\) modulo \(m\) if
An inverse exists iff
When it exists, it is unique modulo \(m\).
Why it exists
If \(\gcd(a,m)=1\), Bézout's theorem gives integers \(s,t\) such that
Therefore
so \(s\) is an inverse of \(a\) modulo \(m\).
4.2 Linear Congruences
Def. A congruence of the form
where \(m\) is positive, is called a linear congruence.
If \(\gcd(a,m)=1\), multiplying both sides by the inverse \(\overline{a}\) to solve the congruence:
4.3 Chinese Remainder Theorem
Let \(m_1,m_2,\dots,m_n\) be pairwise relatively prime positive integers greater than \(1\), and let \(a_1,a_2,\dots,a_n\) be arbitrary integers.
To solve the system
Let \(m=m_1m_2\cdots m_n\) and \(M_k=\dfrac{m}{m_k}\), since \(\gcd(M_k,m_k)=1\), choose \(y_k\) such that
Then a solution is
中国剩余定理类似寻找向量空间中的正交基向量,每一个 \(M_ky_k\) 都满足模 \(m_k\) 同余 1 而模 \(m_{i,i \ne k}\) 同余 0,这样就能直接通过投影得到系数.
Sun-Tsu's Problem
Solve:
Here
Inverses:
Therefore
Back Substitution
A system of congruences can also be solved by rewriting one congruence as an equality and substituting step by step.
Solve:
From \(x\equiv1\pmod5\), write
Substitute into \(x\equiv2\pmod6\):
Thus \(t\equiv5\pmod6\), so \(t=6u+5\).
Then
Substitute into \(x\equiv3\pmod7\):
Thus \(u\equiv6\pmod7\), so \(u=7v+6\).
Hence
Therefore
4.4 CRT Representation of Large Integers
If \(m_1,m_2,\dots,m_n\) are pairwise relatively prime and \(m=m_1m_2\cdots m_n\), then every integer \(a\) with \(0\leq a<m\) can be uniquely represented by
This representation can make large-integer arithmetic easier, because computations modulo different \(m_k\) can be done separately.
Modulo 3 and 4
For integers less than \(12\), use the pair
Then:
CRT in RSA Decryption
RSA decryption computes
where \(N=PQ\) for large primes \(P,Q\).
Instead of computing directly modulo \(N\), compute
and combine the two results with CRT.
This reduces the modulus size and also allows parallel computation.
5. Fermat's Little Theorem and Related Ideas
5.1 Fermat's Little Theorem
If \(p\) is prime and \(p\nmid a\), then
Also we have \(a^{p-2}\times a \equiv1\pmod p\), so \(a^{p-2}\) is an inverse of \(a\) modulo \(p\).
5.2 Pseudoprimes
If \(n\) is composite and
then \(n\) is a pseudoprime to base \(b\)(伪质数).
Example
$341=11\cdot31 $ is a pseudoprime to base \(2\) because \(2^{340}\equiv1\pmod{341}\).
Carmichael Numbers
A Carmichael number is a composite integer \(n\) such that
for every positive integer \(b\) with \(\gcd(b,n)=1\).
\(561=3\cdot11\cdot17\) is the classic example, for every \(b\) coprime to \(561\) we have \(b^{560}\equiv1\pmod{561}\).
5.3 Primitive Roots
Let \(p\) be prime. An integer \(r\in\mathbb{Z}_p\) is a primitive root modulo \(p\)(原根) if every nonzero element of \(\mathbb{Z}_p\) is congruent to a power of \(r\).
Modulo 11
\(2\) is a primitive root modulo \(11\) because:
5.4 Discrete Logarithms
Suppose \(p\) is prime, \(r\) is a primitive root modulo \(p\), and \(a\in\{1,2,\dots,p-1\}\).
If
then \(e\) is the discrete logarithm(离散对数) of \(a\) modulo \(p\) to the base \(r\).
Example
Modulo \(11\) with base \(2\):
- \(2^8\equiv3\pmod{11}\) so \(\log_2 3=8\)
- \(2^4\equiv5\pmod{11}\) so \(\log_2 5=4\)