示例#1
0
        public static MyFraction FractionIdentities(MyFraction fraction)
        {
            double     a = fraction.Numerator;   //double.Parse(fraction.Numerator.ToString());
            double     b = fraction.Denominator; //decimal.Parse(fraction.Denominator.ToString());
            MyFraction identity;

            identity = new MyFraction(1, (b * (a * b + 1)));
            return(identity);
        }
示例#2
0
        public static MyFraction FractionSplitter(MyFraction fraction, out MyFraction outFraction)
        {
            // use Natural Numbers to split fraction into two sub-fractions a, and b
            int[] natNums = new int[] { 100, 60, 24, 20, 12, 8, 6 };
            int   a       = 0;
            int   b       = 0;

            foreach (int item in natNums) //NaturalNumbers)
            {
                // pick the highest NaturalNumber that is less than numerator and subtract from numerator creating n1 and n2
                if (MyEgyptianFractions.CompareThese((int)fraction.Numerator, item) == 1)
                {
                    a = (int)fraction.Numerator - item;
                    b = item;
                    MyFraction fraction1 = new MyFraction(a, (int)fraction.Denominator);
                    outFraction = new MyFraction(b, (int)fraction.Denominator);
                    return(fraction1);
                }
            }
            outFraction = new MyFraction(0, 0);  // returns a detectible done message as a 0 value fraction
            return(fraction);                    // return the orginal fraction because it is the smallest it can be
        }
示例#3
0
        public static List <MyFraction> IdentitiesDriver(MyFraction fraction)
        {
            List <MyFraction> fractionsList = new List <MyFraction>();    //  start the tracking list using 1 / n where n is existing denominator

            if (fraction.Numerator == 1)
            {
                fractionsList.Add(fraction);
                //return fractionsList;
            }
            MyFraction temp = new MyFraction(1, fraction.Denominator);

            fractionsList.Add(temp);                                    //  store the 1/n fraction to the list
            decimal sumTotal = fraction.DecimalEquivalent();            //  use sumTotal to discover remaining work to be done?

            sumTotal -= temp.DecimalEquivalent();                       //  decrement 1/n from sumTotal
            while (sumTotal > 0)
            {
                MyFraction differenceRemaining = fraction.Difference(temp);
                temp = FractionIdentities(differenceRemaining);
                fractionsList.Add(temp);
                sumTotal -= temp.DecimalEquivalent(); // remove 1/n from the total
            }
            return(fractionsList);                    //  no more work to do since fraction is 1/n already
        }
示例#4
0
        public MyFraction Difference(MyFraction other)
        {
            // take two fractions e.g 2/3 and 4/5 and find the difference
            // Difference( 10/15, 12/15) => 2/15
            double productDenominator = this.Denominator * other.Denominator;
            double newFirstNumerator  = this.Numerator * other.Denominator;
            double newSecondNumerator = other.Numerator * this.Denominator;
            double differenceNumerator;

            if (this.Numerator > other.Numerator)
            {
                differenceNumerator = newFirstNumerator - newSecondNumerator;
            }
            else
            {
                differenceNumerator = newSecondNumerator - newFirstNumerator;
            }
            if (productDenominator % differenceNumerator == 0)
            {
                productDenominator /= differenceNumerator;
                differenceNumerator = 1;
            }
            return(new MyFraction(differenceNumerator, productDenominator));
        }