RosettaCodeData/Task/Greatest-common-divisor/JavaScript/greatest-common-divisor-1.js

18 lines
231 B
JavaScript
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
function gcd(a,b) {
2015-11-18 06:14:39 +00:00
a = Math.abs(a);
b = Math.abs(b);
if (b > a) {
var temp = a;
a = b;
b = temp;
}
while (true) {
a %= b;
if (a === 0) { return b; }
b %= a;
if (b === 0) { return a; }
}
2013-04-10 21:29:02 -07:00
}