Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme.md #106

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions 08_Remainder/Remainder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "40f73521",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"同余方程组的解为 x ≡ 23 (mod 105)\n"
]
"metadata": {
"ExecuteTime": {
"end_time": "2024-07-31T03:46:04.473502Z",
"start_time": "2024-07-31T03:46:04.468611Z"
}
],
},
"source": [
"def extended_gcd(a, b):\n",
" \"\"\"\n",
" 扩展欧几里得算法,用于求解 ax + by = gcd(a, b) 的整数解\n",
" :param a: 整数\n",
" :param b: 整数\n",
" :return: (gcd(a, b), x, y)\n",
" \"\"\"\n",
" if b == 0:\n",
" return a, 1, 0\n",
" else:\n",
Expand All @@ -25,7 +26,6 @@
"def chinese_remainder_theorem(congruences):\n",
" \"\"\"\n",
" 中国剩余定理求解函数\n",
"\n",
" :param congruences: 模线性同余方程组,格式为 [(a1, m1), (a2, m2), ..., (an, mn)],表示方程组为 x ≡ ai (mod mi)\n",
" :return: 方程组的解 x\n",
" \"\"\"\n",
Expand All @@ -36,19 +36,33 @@
"\n",
" # 计算 Mi 和 Mi 的模逆元素\n",
" M_values = [M // mi for _, mi in congruences]\n",
" Mi_values = [extended_gcd(Mi, mi)[1] for Mi, (_, mi) in zip(M_values, congruences)]\n",
" inverse_values = [extended_gcd(Mi, mi)[1] % mi for Mi, (_, mi) in zip(M_values, congruences)]\n",
"\n",
" # 计算解 x\n",
" x = sum(ai * Mi * mi for (ai, _), Mi, mi in zip(congruences, Mi_values, M_values)) % M\n",
" x = sum(ai * Mi * inverse for (ai, mi), Mi, inverse in zip(congruences, M_values, inverse_values)) % M\n",
"\n",
" return x\n",
"\n",
"# 示例:解 x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7)\n",
"congruences = [(2, 3), (3, 5), (2, 7)]\n",
"solution = chinese_remainder_theorem(congruences)\n",
"print(f\"同余方程组的解为 x ≡ {solution} (mod {congruences[0][1] * congruences[1][1] * congruences[2][1]})\")\n",
"# 同余方程组的解为 x ≡ 23 (mod 105)"
]
"M_product = 1\n",
"for _, mi in congruences:\n",
" M_product *= mi\n",
"\n",
"print(f\"同余方程组的解为 x ≡ {solution} (mod {M_product})\")\n",
"# 输出: 同余方程组的解为 x ≡ 23 (mod 105)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"同余方程组的解为 x ≡ 23 (mod 105)\n"
]
}
],
"execution_count": 1
},
{
"cell_type": "code",
Expand Down
21 changes: 15 additions & 6 deletions 08_Remainder/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ $a_3 = 2, b_3 = 15, b_3' \equiv 15^{-1} = 1\pmod{7}$

```python
def extended_gcd(a, b):
"""
扩展欧几里得算法,用于求解 ax + by = gcd(a, b) 的整数解
:param a: 整数
:param b: 整数
:return: (gcd(a, b), x, y)
"""
if b == 0:
return a, 1, 0
else:
Expand All @@ -292,7 +298,6 @@ def extended_gcd(a, b):
def chinese_remainder_theorem(congruences):
"""
中国剩余定理求解函数

:param congruences: 模线性同余方程组,格式为 [(a1, m1), (a2, m2), ..., (an, mn)],表示方程组为 x ≡ ai (mod mi)
:return: 方程组的解 x
"""
Expand All @@ -303,18 +308,22 @@ def chinese_remainder_theorem(congruences):

# 计算 Mi 和 Mi 的模逆元素
M_values = [M // mi for _, mi in congruences]
Mi_values = [extended_gcd(Mi, mi)[1] for Mi, (_, mi) in zip(M_values, congruences)]
inverse_values = [extended_gcd(Mi, mi)[1] % mi for Mi, (_, mi) in zip(M_values, congruences)]

# 计算解 x
x = sum(ai * Mi * mi for (ai, _), Mi, mi in zip(congruences, Mi_values, M_values)) % M
x = sum(ai * Mi * inverse for (ai, mi), Mi, inverse in zip(congruences, M_values, inverse_values)) % M

return x

# 示例:解 x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7)
congruences = [(2, 3), (3, 5), (2, 7)]
solution = chinese_remainder_theorem(congruences)
print(f"同余方程组的解为 x ≡ {solution} (mod {congruences[0][1] * congruences[1][1] * congruences[2][1]})")
# 同余方程组的解为 x ≡ 23 (mod 105)
M_product = 1
for _, mi in congruences:
M_product *= mi

print(f"同余方程组的解为 x ≡ {solution} (mod {M_product})")
# 输出: 同余方程组的解为 x ≡ 23 (mod 105)
```

### 4.5 逆向使用
Expand All @@ -337,4 +346,4 @@ $$

## 5. 总结

这一讲,我们学习了剩余类,同余方程组,和中国剩余定理。其中中国剩余定理不仅可以解同余方程组,还可以逆向使用,将大问题分解为小问题,在零知识证明中非常重要。
这一讲,我们学习了剩余类,同余方程组,和中国剩余定理。其中中国剩余定理不仅可以解同余方程组,还可以逆向使用,将大问题分解为小问题,在零知识证明中非常重要。