That's not a recursive implementation of gcd
, the function is not calling itself and besides it's using a while
loop. A truly recursive approach will look like this:
int gcd(int x, int y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
Or a bit shorter:
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
The above implementation is based on the Euclidean algorithm, refer to this for more details.