public void SimplifyTest()
 {
     Fraction target = new Fraction(10, 2);
     target.Simplify();
     Assert.AreEqual(5, target.numerator);
     Assert.AreEqual(1, target.denominator);
 }
Exemplo n.º 2
0
        static Boolean Cancels(Fraction frac)
        {
            if (frac.numerator % 10 == 0 && frac.denominator % 10 == 0)
            {
                // trivial case, zeroes cancel
                return false;
            }

            double realFraction = Convert.ToDouble(frac.numerator) / Convert.ToDouble(frac.denominator);
            string ns = frac.numerator.ToString(),
                   ds = frac.denominator.ToString();

            foreach (char c in ns)
            {
                int n = System.Convert.ToInt16("" + c);
                string n1 = ns.Replace("" + c, ""),
                       d1 = ds.Replace("" + c, "");
                if (n1.Length == 0 || d1.Length == 0) { continue; }
                int ni = System.Convert.ToInt32(n1),
                    di = System.Convert.ToInt32(d1);
                if (di == 0) { continue; }
                double f = ni / Convert.ToDouble(di);
                if (f == realFraction)
                {
                    return true;
                }
            }
            return false;
        }
 public void op_MultiplyTest()
 {
     Fraction a = new Fraction(1, 2);
     Fraction b = new Fraction(2, 1);
     Fraction actual;
     actual = (a * b);
     Assert.AreEqual(2, actual.numerator);
     Assert.AreEqual(2, actual.denominator);
 }
Exemplo n.º 4
0
 Fraction Expand(long n)
 {
     // we're going to cache the previous one otherwise, with the
     // raw fractional numbers getting massive, the arithmetic takes
     // forever
     if (n > 1)
     {
         prev = 2 + prev;
     }
     prev = 1 / prev;
     return 1 + prev;
 }
Exemplo n.º 5
0
        public static Fraction operator +(Fraction a, Fraction b)
        {
            BigInteger denom = a.denominator;
            BigInteger n1 = a.numerator, n2 = b.numerator;
            if (denom != b.denominator)
            {
                denom = a.denominator * b.denominator;
                n1 = checked(n1 * b.denominator);
                n2 = checked(n2 * a.denominator);
            }

            Fraction ret = new Fraction(n1 + n2, denom);
            ret.Simplify();
            return ret;
        }
        public void op_DivisionTest()
        {
            Fraction expected = new Fraction(1, 2);
            Fraction actual = new Fraction(1) / new Fraction(2);
            Assert.AreEqual(true, expected == actual);

            expected = new Fraction(3, 2);
            actual = new Fraction(3) / new Fraction(2);
            Assert.AreEqual(true, expected == actual);

            actual = new Fraction(1, 2) / new Fraction(1, 6);
            actual.Simplify();
            expected = new Fraction(3, 1);
            Assert.AreEqual(true, expected == actual);
        }
Exemplo n.º 7
0
 static void Main(string[] args)
 {
     Fraction product = new Fraction(1);
     for (int a = 10; a < 100; a++)
     {
         for (int b = a+1; b < 100; b++)
         {
             Fraction f = new Fraction(a, b);
             if (Cancels(f))
             {
                 product = product * f;
             }
         }
     }
     product.Simplify();
     Euler.Utils.OutputAnswer(product.denominator.ToString());
 }
Exemplo n.º 8
0
        public override void Process()
        {
            for (int i = 12; i < 100; i++)
            {
                for (int j = i + 1; j < 100; j++)
                {
                    // Ignore trivial combinations
                    if (IsTrivialCombination(i, j))
                        continue;

                    int x = RemoveCommonNumber(i, j);
                    int y = RemoveCommonNumber(j, i);

                    // lets not divide by zero...
                    if (x <= 0 || y <= 0)
                        continue;

                    // Compare original fraction with 'simplified' fraction
                    Fraction original = new Fraction(i, j);
                    Fraction cancelled = new Fraction(x, y);

                    if (original == cancelled)
                    {
                        fractions.Add(original);
                    }
                }
            }

            // Multiply the fractions together
            long numerator = fractions.Select(x => (long)x.Numerator).Aggregate((x, y) => x * y);
            long denominator = fractions.Select(x => (long)x.Denominator).Aggregate((x, y) => x * y);
            long commonDivisor = FindGreatestCommonDivisor(numerator, denominator);

            // Output findings
            Console.WriteLine("Found {0} fractions:\n", fractions.Count());
            foreach (Fraction fraction in fractions)
            {
                Console.Write(fraction.ToString());
                if (fraction != fractions.Last())
                    Console.Write(", ");
            }

            Console.WriteLine("\nLowest common denominator: {0}", denominator / commonDivisor);
        }