private Rational(BigInteger top, BigInteger bottom, bool reduced) { _top = top; _bottom = bottom; if (bottom == 0) { throw new DivideByZeroException(); } else if (bottom < 0) { _top = -_top; _bottom = -_bottom; } if (!reduced) { BigInteger gcd = BigInteger.GreatestCommonDivisor(_top, _bottom); if (gcd != BigInteger.One) { _top /= gcd; _bottom /= gcd; } } }
/// <summary> /// Returns the least common multiple (<c>lcm</c>) of two big integers. /// </summary> /// <param name="a">First Integer: a.</param> /// <param name="b">Second Integer: b.</param> /// <returns>Least common multiple <c>lcm</c>(a,b)</returns> public static BigInteger LeastCommonMultiple(BigInteger a, BigInteger b) { if (a.IsZero || b.IsZero) { return(BigInteger.Zero); } return(BigInteger.Abs((a / BigInteger.GreatestCommonDivisor(a, b)) * b)); }
/// <summary> /// Returns the greatest common divisor (<c>gcd</c>) of two big integers. /// </summary> /// <param name="a">First Integer: a.</param> /// <param name="b">Second Integer: b.</param> /// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns> public static BigInteger GreatestCommonDivisor(BigInteger a, BigInteger b) { return(BigInteger.GreatestCommonDivisor(a, b)); }
public static Int Lcm(this Int value1, Int value2) { return(value1 * value2 / Int.GreatestCommonDivisor(value1, value2)); }
public BigInt gcd(BigInt x) { return(FSBigInt.GreatestCommonDivisor(_num, x._num)); }