A decent gcd code

By | June 30, 2016
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

This is a very decent code to calculate gcd. I found this when I was doing the water and jug problem.

public static int gcd(int x, int y) {
    while (y != 0) {
        int tmp = y;
        y = x % y;
        x = tmp;
    }
    return x;
}