You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algebra/chinese-remainder-theorem.md
+30-24Lines changed: 30 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,67 +1,75 @@
1
-
<!--?title Chinese Remainder Theorem -->
2
-
1
+
---
2
+
title: Chinese Remainder Theorem
3
+
hide:
4
+
- navigation
5
+
---
3
6
# Chinese Remainder Theorem
4
7
5
8
The Chinese Remainder Theorem (which will be referred to as CRT in the rest of this article) was discovered by Chinese mathematician Sun Zi.
6
9
7
10
## Formulation
8
11
9
-
Let $p = p_1 p_2 \cdots p_k$, where $p_i$ are pairwise relatively prime. In addition to $p_i$, we are also given a set of congruence equations
12
+
Let $p = p_1 \cdot p_2 \cdots p_k$, where $p_i$ are pairwise relatively prime. In addition to $p_i$, we are also given a set of congruence equations
13
+
10
14
$$\begin{align}
11
15
a &\equiv a_1 \pmod{p_1} \\\\
12
16
a &\equiv a_2 \pmod{p_2} \\\\
13
17
&\ldots \\\\
14
18
a &\equiv a_k \pmod{p_k}
15
19
\end{align}$$
20
+
16
21
where $a_i$ are some given constants. The original form of CRT then states that the given set of congruence equations always has *one and exactly one* solution modulo $p$.
17
22
18
23
### Corollary
19
24
20
25
A consequence of the CRT is that the equation
21
-
$$
22
-
x \equiv a \pmod{p}
23
-
$$
26
+
27
+
$$x \equiv a \pmod{p}$$
28
+
24
29
is equivalent to the system of equations
30
+
25
31
$$\begin{align}
26
32
x &\equiv a_1 \pmod{p_1} \\\\
27
33
&\ldots \\\\
28
34
x &\equiv a_k \pmod{p_k}
29
35
\end{align}$$
36
+
30
37
(As above, assume that $p = p_1 p_2 \cdots p_k$ and $p_i$ are pairwise relatively prime).
31
38
32
39
## Garner's Algorithm
33
40
34
41
Another consequence of the CRT is that we can represent big numbers using an array of small integers. For example, let $p$ be the product of the first $1000$ primes. From calculations we can see that $p$ has around $3000$ digits.
35
42
36
43
Any number $a$ less than $p$ can be represented as an array $a_1, \ldots, a_k$, where $a_i \equiv a \pmod{p_i}$. But to do this we obviously need to know how to get back the number $a$ from its representation. In this section, we discuss Garner's Algorithm, which can be used for this purpose. We seek a representation on the form
which is called the mixed radix representation of $a$.
41
48
Garner's algorithm computes the coefficients $x_1, \ldots, x_k$.
42
49
43
50
Let $r_{ij}$ denote the inverse of $p_i$ modulo $p_j$
44
-
$$
45
-
r_{ij} = (p_i)^{-1} \pmod{p_j}
46
-
$$
51
+
52
+
$$r_{ij} = (p_i)^{-1} \pmod{p_j}$$
53
+
47
54
which can be found using the algorithm described in [Modular Inverse](module-inverse.md). Substituting $a$ from the mixed radix representation into the first congruence equation we obtain
48
-
$$
49
-
a_1 \equiv x_1 \pmod{p_1}.
50
-
$$
55
+
56
+
$$a_1 \equiv x_1 \pmod{p_1}.$$
57
+
51
58
Substituting into the second equation yields
52
-
$$
53
-
a_2 \equiv x_1 + x_2 p_1 \pmod{p_2}.
54
-
$$
59
+
60
+
$$a_2 \equiv x_1 + x_2 p_1 \pmod{p_2}.$$
61
+
55
62
which can be rewritten by subtracting $x_1$ and dividing by $p_1$ to get
It is worth noting that in practice, we almost always need to compute the answer using Big Integers, but the coefficients $x_i$ can usually be calculated using built-in types, and therefore Garner's algorithm is very efficient.
Copy file name to clipboardExpand all lines: src/algebra/discrete-log.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,8 @@
1
-
<!--?title Discrete Logarithm -->
1
+
---
2
+
title: Discrete Logarithm
3
+
hide:
4
+
- navigation
5
+
---
2
6
3
7
# Discrete Logarithm
4
8
@@ -127,7 +131,7 @@ Instead of a `map`, we can also use a hash table (`unordered_map` in C++) which
127
131
Problems often ask for the minimum $x$ which satisfies the solution.
128
132
It is possible to get all answers and take the minimum, or reduce the first found answer using [Euler's theorem](phi-function.md#toc-tgt-2), but we can be smart about the order in which we calculate values and ensure the first answer we find is the minimum.
129
133
130
-
```cpp discrete_log
134
+
```{.cppfile=discrete_log}
131
135
// Returns minimum x for which a ^ x % m = b % m, a and m are coprime.
132
136
intsolve(int a, int b, int m) {
133
137
a %= m, b %= m;
@@ -156,12 +160,13 @@ int solve(int a, int b, int m) {
156
160
157
161
The complexity is $O(\sqrt{m})$ using `unordered_map`.
158
162
159
-
## When $a$ and $m$ are not coprime
163
+
## When $a$ and $m$ are not coprime { data-toc-label='When a and m are not coprime' }
160
164
Let $g = \gcd(a, m)$, and $g > 1$. Clearly $a^x \bmod m$ for every $x \ge 1$ will be divisible by $g$.
161
165
162
166
If $g \nmid b$, there is no solution for $x$.
163
167
164
168
If $g \mid b$, let $a = g \alpha, b = g \beta, m = g \nu$.
169
+
165
170
$$
166
171
\begin{aligned}
167
172
a^x & \equiv b \mod m \\\
@@ -172,7 +177,7 @@ $$
172
177
173
178
The baby-step giant-step algorithm can be easily extended to solve $ka^{x} \equiv b \pmod m$ for $x$.
Copy file name to clipboardExpand all lines: src/algebra/divisors.md
+16-5Lines changed: 16 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,8 @@
1
-
<!--?title Number of divisors / sum of divisors -->
1
+
---
2
+
title: Number of divisors / sum of divisors
3
+
hide:
4
+
- navigation
5
+
---
2
6
# Number of divisors / sum of divisors
3
7
4
8
In this article we discuss how to compute the number of divisors $d(n)$ and the sum of divisors $\sigma(n)$ of a given number $n$.
@@ -15,6 +19,7 @@ If a prime factor $p$ appears $e$ times in the prime factorization of $n$, then
15
19
Which means we have $e+1$ choices.
16
20
17
21
Therefore if the prime factorization of $n$ is $p_1^{e_1} \cdot p_2^{e_2} \cdots p_k^{e_k}$, where $p_i$ are distinct prime numbers, then the number of divisors is:
Copy file name to clipboardExpand all lines: src/algebra/factorial-modulo.md
+13-9Lines changed: 13 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,8 @@
1
-
<!--?title Factorial modulo P -->
1
+
---
2
+
title: Factorial modulo P
3
+
hide:
4
+
- navigation
5
+
---
2
6
# Factorial modulo $p$
3
7
4
8
In some cases it is necessary to consider complex formulas modulo some prime $p$, containing factorials in both numerator and denominator, like such that you encounter in the formula for Binomial coefficients.
@@ -9,26 +13,26 @@ But in fractions the factors of $p$ can cancel, and the resulting expression wil
9
13
10
14
Thus, formally the task is: You want to calculate $n! \bmod p$, without taking all the multiple factors of $p$ into account that appear in the factorial.
11
15
Imaging you write down the prime factorization of $n!$, remove all factors $p$, and compute the product modulo $p$.
12
-
We will denote this *modified* factorial with $n!\_{\%p}$.
Learning how to effectively calculate this modified factorial allows us to quickly calculate the value of the various combinatorial formulas (for example, [Binomial coefficients](../combinatorics/binomial-coefficients.md)).
0 commit comments