mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 13:37:48 +00:00
factor::miller_rabin: Remove duplicated work
The duplicate work was introduced in 2015.
This commit is contained in:
parent
8cda0f596e
commit
e415b17c01
1 changed files with 5 additions and 14 deletions
|
@ -54,7 +54,7 @@ pub(crate) fn test<A: Arithmetic + Basis>(m: A) -> Result {
|
|||
let one = m.one();
|
||||
let minus_one = m.minus_one();
|
||||
|
||||
for _a in A::BASIS.iter() {
|
||||
'witness: for _a in A::BASIS.iter() {
|
||||
let _a = _a % n;
|
||||
if _a == 0 {
|
||||
continue;
|
||||
|
@ -65,22 +65,11 @@ pub(crate) fn test<A: Arithmetic + Basis>(m: A) -> Result {
|
|||
// x = a^r mod n
|
||||
let mut x = m.pow(a, r);
|
||||
|
||||
{
|
||||
// y = ((x²)²...)² i times = x ^ (2ⁱ) = a ^ (r 2ⁱ) = x ^ (n - 1)
|
||||
let mut y = x;
|
||||
for _ in 0..i {
|
||||
y = m.mul(y, y)
|
||||
}
|
||||
if y != one {
|
||||
return Pseudoprime;
|
||||
};
|
||||
}
|
||||
|
||||
if x == one || x == minus_one {
|
||||
continue;
|
||||
}
|
||||
|
||||
loop {
|
||||
for _ in 1..i {
|
||||
let y = m.mul(x, x);
|
||||
if y == one {
|
||||
return Composite(gcd(m.to_u64(x) - 1, m.modulus()));
|
||||
|
@ -88,10 +77,12 @@ pub(crate) fn test<A: Arithmetic + Basis>(m: A) -> Result {
|
|||
if y == minus_one {
|
||||
// This basis element is not a witness of `n` being composite.
|
||||
// Keep looking.
|
||||
break;
|
||||
continue 'witness;
|
||||
}
|
||||
x = y;
|
||||
}
|
||||
|
||||
return Pseudoprime;
|
||||
}
|
||||
|
||||
Prime
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue