Example #1
0
        /// <summary>
        /// Returns a new fraction with reduced values, if values can be reduced.
        /// </summary>
        /// <param name="SourceFraction">Fraction you want to reduce.</param>
        /// <returns>Reduced fraction.  i.e., a source fraction with a value of 5/10 will return a new fraction of 1/2.</returns>
        public static Fraction Reduce(Fraction SourceFraction)
        {
            int gcf = Fraction.GreatestCommonFactor(SourceFraction);

            int num = SourceFraction.Numerator / gcf;
            int den = SourceFraction.Denominator / gcf;

            return(new Fraction(num, den));
        }
Example #2
0
        /// <summary>
        /// Returns the lowest common denominator between 2 fractions.
        /// </summary>
        /// <param name="Fraction1">First fraction</param>
        /// <param name="Fraction2">Second fraction</param>
        /// <returns>The smallest common multiple between the given fractions.</returns>
        public static int LowestCommonDenominator(Fraction Fraction1, Fraction Fraction2)
        {
            Fraction f1 = Fraction1;
            Fraction f2 = Fraction2;

            if (f1.Denominator > f2.Denominator)
            {
                if (f1.Denominator % f2.Denominator == 0)
                {
                    if (f1.Denominator > Fraction.GreatestCommonFactor(f1.Denominator, f2.Denominator))
                    {
                        return(f1.Denominator);
                    }
                    else
                    {
                        return(f1.Denominator / Fraction.GreatestCommonFactor(f1.Denominator, f2.Denominator));
                    }
                }
            }

            if (f2.Denominator > f1.Denominator)
            {
                if (f2.Denominator % f1.Denominator == 0)
                {
                    if (f2.Denominator > Fraction.GreatestCommonFactor(f1.Denominator, f2.Denominator))
                    {
                        return(f2.Denominator);
                    }
                    else
                    {
                        return(f2.Denominator / Fraction.GreatestCommonFactor(f1.Denominator, f2.Denominator));
                    }
                }
            }

            return((f1.Denominator * f2.Denominator) / Fraction.GreatestCommonFactor(f1.Denominator, f2.Denominator));
        }
Example #3
0
 /// <summary>
 /// Returns the greatest common factor between the numerator &amp; denominator of a fraction.
 /// </summary>
 /// <param name="inputFraction">The fraction to find the GCF for.</param>
 /// <returns>The largest integer that these numbers can both be evenly divided by.</returns>
 public static int GreatestCommonFactor(Fraction inputFraction)
 {
     return(Fraction.GreatestCommonFactor(inputFraction.Numerator, inputFraction.Denominator));
 }