Skip to content

Commit

Permalink
Optimized performance of the GCD algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed Feb 17, 2014
1 parent cc0b29b commit cf2ef99
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions algorithms/math/gcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
* @return Number
*/
var gcd = function (a, b) {
var tmp = a;
a = Math.max(a, b);
b = Math.min(tmp, b);
if (a % b === 0) return b;

while (b !== 0) {
if (a > b) {
a -= b;
Expand Down

0 comments on commit cf2ef99

Please sign in to comment.