Exemplo n.º 1
0
        public void Fractios_SimplifyTest()
        {
            Assert.IsFalse(new Fraction(1, 2).CanBeSimplified());
            Assert.IsFalse(new Fraction(1, 3).CanBeSimplified());
            Assert.IsFalse(new Fraction(2, 3).CanBeSimplified());
            Assert.IsFalse(new Fraction(59, 58).CanBeSimplified());

            Assert.IsTrue(new Fraction(2, 4).CanBeSimplified());
            Assert.IsTrue(new Fraction(1000, 100).CanBeSimplified());
            Assert.IsTrue(new Fraction(63, 9).CanBeSimplified());
            Assert.IsTrue(new Fraction(121, 11).CanBeSimplified());

            Fraction testOutput;
            testOutput = new Fraction(8, 12).Simplify();
            Assert.AreEqual(testOutput.Numerator, 2);
            Assert.AreEqual(testOutput.Denominator, 3);

            testOutput = new Fraction(24, 108).Simplify();
            Assert.AreEqual(testOutput.Numerator, 2);
            Assert.AreEqual(testOutput.Denominator, 9);
        }
Exemplo n.º 2
0
        public long Solve()
        {
            var curiousFractions = new List<Fraction>();

            for (int numeratorDigit1 = 1; numeratorDigit1 < 10; numeratorDigit1++)
            {
                for (int numeratorDigit2 = 1; numeratorDigit2 < 10; numeratorDigit2++)
                {
                    for (int denominatorDigit1 = 1; denominatorDigit1 < 10; denominatorDigit1++)
                    {
                        for (int denominatorDigit2 = 1; denominatorDigit2 < 10; denominatorDigit2++)
                        {
                            if (numeratorDigit1 == denominatorDigit1 && numeratorDigit2 == denominatorDigit2)
                                continue;

                            var numerator = numeratorDigit1 * 10 + numeratorDigit2;
                            var denominator = denominatorDigit1 * 10 + denominatorDigit2;
                            if (numerator >= denominator)
                                continue; // only less than one

                            var currentFraction = new Fraction(numerator, denominator);

                            if (numeratorDigit1 == denominatorDigit1 && new Fraction(numeratorDigit2, denominatorDigit2).Equals(currentFraction)
                                || numeratorDigit1 == denominatorDigit2 && new Fraction(numeratorDigit2, denominatorDigit1).Equals(currentFraction)
                                || numeratorDigit2 == denominatorDigit1 && new Fraction(numeratorDigit1, denominatorDigit2).Equals(currentFraction)
                                || numeratorDigit2 == denominatorDigit2 && new Fraction(numeratorDigit1, denominatorDigit1).Equals(currentFraction))
                            {
                                Console.WriteLine("Found curious fraction: {0}", currentFraction);
                                curiousFractions.Add(currentFraction);
                            }
                        }
                    }
                }
            }

            var product = curiousFractions.Aggregate(new Fraction(1, 1), (f1, f2) => f1 * f2);

            return product.Simplify().Denominator;
        }
Exemplo n.º 3
0
 public bool EqualsStrict(Fraction otherFraction)
 {
     return this.Numerator == otherFraction.Numerator
         && this.Denominator == otherFraction.Denominator;
 }