refactor: Change gcd implementation to attempt faster calc

This commit is contained in:
Alivecow 2024-11-28 17:45:10 +01:00
parent f7f3c44acb
commit 270abdb7b7
2 changed files with 22 additions and 10 deletions

View file

@ -576,9 +576,21 @@ pub fn gcd(a: &Polynomial, b: &Polynomial) -> Polynomial {
if a.is_zero() {
return b.clone();
}
if b.is_zero() {
return a.clone();
}
let monic_b = b.div(&a).1.monic();
return gcd(&monic_b, a);
if a.degree() > b.degree() {
return gcd(b, a);
}
let (_, remainder) = b.div(a);
if remainder.is_zero() {
return a.clone().monic();
}
gcd(&remainder, a)
}
pub fn non_monic_gcd(a: &Polynomial, b: &Polynomial) -> Polynomial {