Mono.Math.BigInteger.Kernel.gcd C# (CSharp) Method

gcd() public static method

public static gcd ( BigInteger a, BigInteger b ) : BigInteger
a BigInteger
b BigInteger
return BigInteger
			public static BigInteger gcd (BigInteger a, BigInteger b)
			{
				BigInteger x = a;
				BigInteger y = b;

				BigInteger g = y;

				while (x.length > 1) {
					g = x;
					x = y % x;
					y = g;

				}
				if (x == 0) return g;

				// TODO: should we have something here if we can convert to long?

				//
				// Now we can just do it with single precision. I am using the binary gcd method,
				// as it should be faster.
				//

				uint yy = x.data [0];
				uint xx = y % yy;

				int t = 0;

				while (((xx | yy) & 1) == 0) {
					xx >>= 1; yy >>= 1; t++;
				}
				while (xx != 0) {
					while ((xx & 1) == 0) xx >>= 1;
					while ((yy & 1) == 0) yy >>= 1;
					if (xx >= yy)
						xx = (xx - yy) >> 1;
					else
						yy = (yy - xx) >> 1;
				}

				return yy << t;
			}