Skip to content

Commit

Permalink
small gcd cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Feb 8, 2018
1 parent f5c0546 commit 4714f82
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,13 +945,13 @@ impl Integer for BigUint {
fn gcd(&self, other: &Self) -> Self {
// Stein's algorithm
if self.is_zero() {
return (*other).clone();
return other.clone();
}
if other.is_zero() {
return (*self).clone();
return self.clone();
}
let mut m = (*self).clone();
let mut n = (*other).clone();
let mut m = self.clone();
let mut n = other.clone();

// find common factors of 2
let shift = cmp::min(
Expand All @@ -963,14 +963,10 @@ impl Integer for BigUint {
// m inside loop
n >>= n.trailing_zeros();

loop {
while !m.is_zero() {
m >>= m.trailing_zeros();
if n > m { mem::swap(&mut n, &mut m) }
m -= &n;

if m.is_zero() {
break
}
}

n << shift
Expand Down

0 comments on commit 4714f82

Please sign in to comment.