apply TCO to gcd function.