by feliperibeiro · 12y ago · 0 comments
34 var tmp = a;
35 a = Math.max(a, b);
36 b = Math.min(tmp, b);
37 if (a % b === 0) return b;
to avoid the unnecessary repetitions if b is already the GCDEdit: Also, if you stick to the original subtraction-based version of the algorithm, why adding that other optimization? That wasn't part of the original algorithm, either. This seems to be double standard.
gcd(a, b) = gcd(b, a mod b),
and there is no need to compute mod with anything other than %.https://github.com/felipernb/algorithms.js/commit/e5a04f9ad0...
I'm surprised the author didn't implement the binary gcd, however, although it has the same big-O as Euclid: https://en.wikipedia.org/wiki/Binary_GCD_algorithm