The function below takes two integers and calculates the greatest common divisor (gcd):-
function mygcd(x, y) {
while(y) var t = y, y = x % y, x = t;
return x;
}
I've run through a load of test cases and stepped through the code to see how it comes to its result, but I'm absolutely stumped in terms of the method - can anybody explain to me in simple terms how this works, and how the while loop eventually returns the GCD of the two values x and y?
NOTE: I do understand how the code itself works - what I want to know is how the calculations eventually return the correct result.