/// <summary> /// Calculates least common multiple of two integer numbers. /// </summary> /// <param name="a">First number.</param> /// <param name="b">Second number.</param> /// <returns>Least common multiple of <paramref name="a"/> and <paramref name="b"/>.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="a"/> is zero or negative. -or- /// <paramref name="b"/> is zero or negative.</exception> internal static long LeastCommonMultiple(long a, long b) { ThrowIfArgument.IsNonpositive(nameof(a), a, "First number is zero or negative."); ThrowIfArgument.IsNonpositive(nameof(b), b, "Second number is zero or negative."); long n1, n2; if (a > b) { n1 = a; n2 = b; } else { n1 = b; n2 = a; } for (int i = 1; i < n2; i++) { if ((n1 * i) % n2 == 0) { return(i * n1); } } return(n1 * n2); }
/// <summary> /// Divides the specified <see cref="Fraction"/> by positive integer number. /// </summary> /// <param name="fraction">The dividend.</param> /// <param name="number">The divisor.</param> /// <returns>Quotient of <paramref name="fraction"/> and <paramref name="number"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="fraction"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="number"/> is zero or negative.</exception> public static Fraction operator /(Fraction fraction, long number) { ThrowIfArgument.IsNull(nameof(fraction), fraction); ThrowIfArgument.IsNonpositive(nameof(number), number, "Number is zero or negative."); return(new Fraction(fraction.Numerator, fraction.Denominator * number)); }
/// <summary> /// Initializes a new instance of the <see cref="Fraction"/> with the specified /// numerator and denominator. /// </summary> /// <param name="numerator">The numerator of a fraction.</param> /// <param name="denominator">The denominator of a fraction.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="numerator"/> is negative. -or- /// <paramref name="denominator"/> is zero or negative.</exception> public Fraction(long numerator, long denominator) { ThrowIfArgument.IsNegative(nameof(numerator), numerator, "Numerator is negative."); ThrowIfArgument.IsNonpositive(nameof(denominator), denominator, "Denominator is zero or negative."); // Simplify a fraction var greatestCommonDivisor = MathUtilities.GreatestCommonDivisor(numerator, denominator); numerator /= greatestCommonDivisor; denominator /= greatestCommonDivisor; Numerator = numerator; Denominator = denominator; }