/// <summary> /// Gets the greatest common divisor of two elements in a <see cref="IEuclideanDomain{T, TFirst, TSecond}"/> via Euclid's algorithm. The GCD of two elements <c>X</c> and <c>Y</c> is the unique minimal principal ideal. /// </summary> /// <typeparam name="T">The type of the carrier set.</typeparam> /// <typeparam name="TFirst">The type of the first grouplike operation.</typeparam> /// <typeparam name="TSecond">The type of the second groupike operation.</typeparam> /// <param name="r">The ringlike structure.</param> /// <param name="x">The first element.</param> /// <param name="y">The second element.</param> public static T Gcd <T, TFirst, TSecond>(this IEuclideanDomain <T, TFirst, TSecond> r, T x, T y) where TFirst : ICommutativeGroup <T> where TSecond : IMonoid <T>, ICommutative <T> { if (r.IsZero(x) || r.IsZero(y)) { return(r.Zero <T, TFirst>()); } while (!r.IsZero(y)) { var t = y; y = r.EuclideanDivide(x, y).remainder; x = t; } return(x); }