Skip to content

Commit 093d331

Browse files
committed
some fixes
1 parent 9fae6f6 commit 093d331

33 files changed

+438
-227
lines changed

src/algebra/all-submasks.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<!--?title Submask Enumeration -->
1+
---
2+
title: Submask Enumeration
3+
hide:
4+
- navigation
5+
---
26

37
# Submask Enumeration
48

src/algebra/balanced-ternary.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<!--?title Balanced Ternary-->
1+
---
2+
title: Balanced Ternary
3+
hide:
4+
- navigation
5+
---
26
# Balanced Ternary
37

48
!["Setun computer using Balanced Ternary system"](http://ternary.3neko.ru/photo/setun1_small.jpg)
@@ -54,6 +58,7 @@ Let us process it from the least significant (rightmost) digit:
5458
The final result is `1Z101`.
5559

5660
Let us convert it back to the decimal system by adding the weighted positional values:
61+
5762
$$ 1Z101 = 81 \cdot 1 + 27 \cdot (-1) + 9 \cdot 1 + 3 \cdot 0 + 1 \cdot 1 = 64_{10} $$
5863

5964
**Example 2:** Let us convert `237` to balanced ternary. At first we use normal ternary to rewrite the number:

src/algebra/big-integer.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<!--?title Arbitrary-Precision Arithmetic -->
1+
---
2+
title: Arbitrary
3+
hide:
4+
- navigation
5+
---
26

37
# Arbitrary-Precision Arithmetic
48

src/algebra/chinese-remainder-theorem.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,75 @@
1-
<!--?title Chinese Remainder Theorem -->
2-
1+
---
2+
title: Chinese Remainder Theorem
3+
hide:
4+
- navigation
5+
---
36
# Chinese Remainder Theorem
47

58
The Chinese Remainder Theorem (which will be referred to as CRT in the rest of this article) was discovered by Chinese mathematician Sun Zi.
69

710
## Formulation
811

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+
1014
$$\begin{align}
1115
a &\equiv a_1 \pmod{p_1} \\\\
1216
a &\equiv a_2 \pmod{p_2} \\\\
1317
&\ldots \\\\
1418
a &\equiv a_k \pmod{p_k}
1519
\end{align}$$
20+
1621
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$.
1722

1823
### Corollary
1924

2025
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+
2429
is equivalent to the system of equations
30+
2531
$$\begin{align}
2632
x &\equiv a_1 \pmod{p_1} \\\\
2733
&\ldots \\\\
2834
x &\equiv a_k \pmod{p_k}
2935
\end{align}$$
36+
3037
(As above, assume that $p = p_1 p_2 \cdots p_k$ and $p_i$ are pairwise relatively prime).
3138

3239
## Garner's Algorithm
3340

3441
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.
3542

3643
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
37-
$$
38-
a = x_1 + x_2 p_1 + x_3 p_1 p_2 + \ldots + x_k p_1 \ldots p_{k-1}
39-
$$
44+
45+
$$a = x_1 + x_2 \cdot p_1 + x_3 \cdot p_1 \cdot p_2 + \ldots + x_k \cdot p_1 \cdots p_{k-1}$$
46+
4047
which is called the mixed radix representation of $a$.
4148
Garner's algorithm computes the coefficients $x_1, \ldots, x_k$.
4249

4350
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+
4754
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+
5158
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+
5562
which can be rewritten by subtracting $x_1$ and dividing by $p_1$ to get
63+
5664
$$\begin{array}{rclr}
5765
a_2 - x_1 &\equiv& x_2 p_1 &\pmod{p_2} \\\\
5866
(a_2 - x_1) r_{12} &\equiv& x_2 &\pmod{p_2} \\\\
5967
x_2 &\equiv& (a_2 - x_1) r_{12} &\pmod{p_2}
6068
\end{array}$$
69+
6170
Similarly we get that
62-
$$
63-
x_3 \equiv ((a_3 - x_1) r_{13} - x_2) r_{23} \pmod{p_3}.
64-
$$
71+
72+
$$x_3 \equiv ((a_3 - x_1) r_{13} - x_2) r_{23} \pmod{p_3}.$$
6573

6674
Now, we can clearly see an emerging pattern, which can be expressed by the following code:
6775

@@ -80,9 +88,7 @@ for (int i = 0; i < k; ++i) {
8088

8189
So we learned how to calculate coefficients $x_i$ in $O(k^2)$ time. The number $a$ can now be calculated using the previously mentioned formula
8290

83-
$$
84-
a = x_1 + x_2 p_1 + x_3 p_1 p_2 + \ldots + x_k p_1 \ldots p_{k-1}
85-
$$
91+
$$a = x_1 + x_2 \cdot p_1 + x_3 \cdot p_1 \cdot p_2 + \ldots + x_k \cdot p_1 \cdots p_{k-1}$$
8692

8793
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.
8894

src/algebra/discrete-log.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<!--?title Discrete Logarithm -->
1+
---
2+
title: Discrete Logarithm
3+
hide:
4+
- navigation
5+
---
26

37
# Discrete Logarithm
48

@@ -127,7 +131,7 @@ Instead of a `map`, we can also use a hash table (`unordered_map` in C++) which
127131
Problems often ask for the minimum $x$ which satisfies the solution.
128132
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.
129133

130-
```cpp discrete_log
134+
```{.cpp file=discrete_log}
131135
// Returns minimum x for which a ^ x % m = b % m, a and m are coprime.
132136
int solve(int a, int b, int m) {
133137
a %= m, b %= m;
@@ -156,12 +160,13 @@ int solve(int a, int b, int m) {
156160
157161
The complexity is $O(\sqrt{m})$ using `unordered_map`.
158162
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' }
160164
Let $g = \gcd(a, m)$, and $g > 1$. Clearly $a^x \bmod m$ for every $x \ge 1$ will be divisible by $g$.
161165
162166
If $g \nmid b$, there is no solution for $x$.
163167
164168
If $g \mid b$, let $a = g \alpha, b = g \beta, m = g \nu$.
169+
165170
$$
166171
\begin{aligned}
167172
a^x & \equiv b \mod m \\\
@@ -172,7 +177,7 @@ $$
172177
173178
The baby-step giant-step algorithm can be easily extended to solve $ka^{x} \equiv b \pmod m$ for $x$.
174179
175-
```cpp discrete_log_extended
180+
```{.cpp file=discrete_log_extended}
176181
// Returns minimum x for which a ^ x % m = b % m.
177182
int solve(int a, int b, int m) {
178183
a %= m, b %= m;

src/algebra/discrete-root.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<!--?title Discrete Root -->
1+
---
2+
title: Discrete Root
3+
hide:
4+
- navigation
5+
---
26

37
# Discrete Root
48

src/algebra/divisors.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff 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+
---
26
# Number of divisors / sum of divisors
37

48
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
1519
Which means we have $e+1$ choices.
1620

1721
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:
22+
1823
$$d(n) = (e_1 + 1) \cdot (e_2 + 1) \cdots (e_k + 1)$$
1924

2025
A way of thinking about it is the following:
@@ -40,21 +45,27 @@ p_1^{e_1} & p_1^{e_1} & p_1^{e_1} \cdot p_2 & p_1^{e_1} \cdot p_2^2 & \dots & p_
4045
We can use the same argument of the previous section.
4146

4247
* If there is only one distinct prime divisor $n = p_1^{e_1}$, then the sum is:
43-
$$1 + p_1 + p_1^2 + \dots + p_1^{e_1} = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1}$$
48+
49+
$$1 + p_1 + p_1^2 + \dots + p_1^{e_1} = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1}$$
4450

4551
* If there are two distinct prime divisors $n = p_1^{e_1} \cdot p_2^{e_2}$, then we can make the same table as before.
4652
The only difference is that now we now want to compute the sum instead of counting the elements.
4753
It is easy to see, that the sum of each combination can be expressed as:
48-
$$\left(1 + p_1 + p_1^2 + \dots + p_1^{e_1}\right) \cdot \left(1 + p_2 + p_2^2 + \dots + p_2^{e_2}\right)$$
49-
$$ = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1}$$
54+
55+
$$\left(1 + p_1 + p_1^2 + \dots + p_1^{e_1}\right) \cdot \left(1 + p_2 + p_2^2 + \dots + p_2^{e_2}\right)$$
56+
57+
$$ = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1}$$
5058

5159
* In general, for $n = p_1^{e_1} \cdot p_2^{e_2} \cdots p_k^{e_k}$ we receive the formula:
52-
$$\sigma(n) = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1} \cdots \frac{p_k^{e_k + 1} - 1}{p_k - 1}$$
60+
61+
$$\sigma(n) = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1} \cdots \frac{p_k^{e_k + 1} - 1}{p_k - 1}$$
5362

5463
## Multiplicative functions
5564

5665
A multiplicative function is a function $f(x)$ which satisfies
66+
5767
$$f(a \cdot b) = f(a) \cdot f(b)$$
68+
5869
if $a$ and $b$ are coprime.
5970

6071
Both $d(n)$ and $\sigma(n)$ are multiplicative functions.

src/algebra/factorial-modulo.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<!--?title Factorial modulo P -->
1+
---
2+
title: Factorial modulo P
3+
hide:
4+
- navigation
5+
---
26
# Factorial modulo $p$
37

48
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
913

1014
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.
1115
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}$.
13-
For instance $7!_{\%p} \equiv 1 \cdot 2 \cdot \underbrace{1}\_{3} \cdot 4 \cdot 5 \underbrace{2}\_{6} \cdot 7 \equiv 2 \bmod 3$.
16+
We will denote this *modified* factorial with $n!_{\%p}$.
17+
For instance $7!_{\%p} \equiv 1 \cdot 2 \cdot \underbrace{1}_{3} \cdot 4 \cdot 5 \underbrace{2}_{6} \cdot 7 \equiv 2 \bmod 3$.
1418

1519
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)).
1620

1721
## Algorithm
1822
Let's write this modified factorial explicitly.
1923

2024
$$\begin{eqnarray}
21-
n!_{\%p} &=& 1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot \underbrace{1}\_{p} \cdot (p+1) \cdot (p+2) \cdot \ldots \cdot (2p-1) \cdot \underbrace{2}\_{2p} \\\
22-
& &\quad \cdot (2p+1) \cdot \ldots \cdot (p^2-1) \cdot \underbrace{1}\_{p^2} \cdot (p^2 +1) \cdot \ldots \cdot n \pmod{p} \\\\
23-
&=& 1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot \underbrace{1}\_{p} \cdot 1 \cdot 2 \cdot \ldots \cdot (p-1) \cdot \underbrace{2}\_{2p} \cdot 1 \cdot 2 \\\
24-
& &\quad \cdot \ldots \cdot (p-1) \cdot \underbrace{1}\_{p^2} \cdot 1 \cdot 2 \cdot \ldots \cdot (n \bmod p) \pmod{p}
25+
n!_{\%p} &=& 1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot \underbrace{1}_{p} \cdot (p+1) \cdot (p+2) \cdot \ldots \cdot (2p-1) \cdot \underbrace{2}_{2p} \\\
26+
& &\quad \cdot (2p+1) \cdot \ldots \cdot (p^2-1) \cdot \underbrace{1}_{p^2} \cdot (p^2 +1) \cdot \ldots \cdot n \pmod{p} \\\\
27+
&=& 1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot \underbrace{1}_{p} \cdot 1 \cdot 2 \cdot \ldots \cdot (p-1) \cdot \underbrace{2}_{2p} \cdot 1 \cdot 2 \\\
28+
& &\quad \cdot \ldots \cdot (p-1) \cdot \underbrace{1}_{p^2} \cdot 1 \cdot 2 \cdot \ldots \cdot (n \bmod p) \pmod{p}
2529
\end{eqnarray}$$
2630

2731
It can be clearly seen that factorial is divided into several blocks of same length except for the last one.
2832

2933
$$\begin{eqnarray}
30-
n!_{\%p}&=& \underbrace{1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot 1}\_{1\text{st}} \cdot \underbrace{1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot 2}\_{2\text{nd}} \cdot \ldots \\\\
31-
& & \cdot \underbrace{1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot 1}\_{p\text{th}} \cdot \ldots \cdot \quad \underbrace{1 \cdot 2 \cdot \cdot \ldots \cdot (n \bmod p)}\_{\text{tail}} \pmod{p}.
34+
n!_{\%p}&=& \underbrace{1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot 1}_{1\text{st}} \cdot \underbrace{1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot 2}_{2\text{nd}} \cdot \ldots \\\\
35+
& & \cdot \underbrace{1 \cdot 2 \cdot 3 \cdot \ldots \cdot (p-2) \cdot (p-1) \cdot 1}_{p\text{th}} \cdot \ldots \cdot \quad \underbrace{1 \cdot 2 \cdot \cdot \ldots \cdot (n \bmod p)}_{\text{tail}} \pmod{p}.
3236
\end{eqnarray}$$
3337

3438
The main part of the blocks it is easy to count — it's just $(p-1)!\ \mathrm{mod}\ p$.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy