Skip to content

Commit

Permalink
fix(math/number-theory/prime.md): Miller Rabin 测试范围 [2, n-2] (#5672)
Browse files Browse the repository at this point in the history
* Miller Rabin 测试范围 [2, n-2]

来源https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Miller%E2%80%93Rabin_test

* style: format markdown files with remark-lint

* Apply suggestions from code review

---------

Co-authored-by: 24OI-bot <15963390+24OI-bot@users.noreply.github.com>
Co-authored-by: Tifa <62847935+Tiphereth-A@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 21, 2024
1 parent 02a3a09 commit 869d4e8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions docs/math/number-theory/prime.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ Carmichael 数有如下性质:
// test_time 为测试次数,建议设为不小于 8
// 的整数以保证正确率,但也不宜过大,否则会影响效率
for (int i = 0; i < test_time; ++i) {
int a = rand() % (n - 2) + 2, v = quickPow(a, u, n);
// 0, 1, n-1 可以直接通过测试, a 取值范围 [2, n-2]
int a = rand() % (n - 3) + 2, v = quickPow(a, u, n);
if (v == 1) continue;
int s;
for (s = 0; s < t; ++s) {
Expand All @@ -277,7 +278,8 @@ Carmichael 数有如下性质:
# test_time 为测试次数,建议设为不小于 8
# 的整数以保证正确率,但也不宜过大,否则会影响效率
for i in range(test_time):
a = random.randint(2, n - 1)
# 0, 1, n-1 可以直接通过测试, a 取值范围 [2, n-2]
a = random.randint(2, n - 2)
v = pow(a, u, n)
if v == 1:
continue
Expand All @@ -304,7 +306,7 @@ Carmichael 数有如下性质:

- 所有的数都要取一遍,不能只选小于 $n$ 的;
- 把 $a$ 换成 $a \bmod n$;
- 如果 $a \equiv 0 \pmod n$,则直接通过该轮测试。
- 如果 $a \equiv 0 \pmod n$ 或 $a \equiv \pm 1 \pmod n$,则直接通过该轮测试。

## 反素数

Expand Down

0 comments on commit 869d4e8

Please sign in to comment.