Exemple #1
0
        public static void Sample_Sqrt_of_2()
        {
            //this example shows how to use ContinuedFraction to solve problem

            //In the first 1000 expansions of sqrt of 2,
            //how many fractions contain a numerator with more digits than denominator?
            //(Question from http://projecteuler.net)
            var sqrtOf2 = new ContinuedFraction(2);

            Console.WriteLine(sqrtOf2.Take(1001).Count(r => r.Numerator.ToString().Length > r.Denominator.ToString().Length));
        }
Exemple #2
0
        public static void Sequence()
        {
            //this example shows how to iterate ContinuedFraction

            //create ContinuedFraction
            var sqrtOf2 = new ContinuedFraction(2);

            //ContinuedFraction derived from BaseSequence you can iterate as sequence
            foreach (var r in sqrtOf2.Take(10))
            {
                Console.WriteLine("{0:0.000000}: {1}", (double)r, r);
            }
        }
Exemple #3
0
        public static void Create()
        {
            //this example shows how to create ContinuedFraction

            //you can create continued fraction by new ContinuedFraction(x, y, z)
            //where formula is (Sqrt(x) + y) / z
            //if you omit y, 0 will be assumed
            //if you omit z, 1 will be assumed
            var sqrtOf2 = new ContinuedFraction(2);

            //show result
            Console.WriteLine(sqrtOf2);
        }
Exemple #4
0
        static List <BigInteger[]> computeQuadratic(BigInteger a, BigInteger b, BigInteger c, int times, Func <Rational, Rational, Rational[]> selector)
        {
            var a2 = 2 * a;
            var s  = b * b - 4 * a * c;

            Rational[] rt;
            var        list = new List <BigInteger[]>();

            var cf = new ContinuedFraction(s, -b, a2);

            foreach (var r in cf.Take(1 + cf.SubQuotients.Count + ((cf.PeriodicLength % 2 == 0) ? 1 : 2) * times * cf.PeriodicLength))
            {
                if (a * r.Numerator * r.Numerator + b * r.Numerator * r.Denominator + c * r.Denominator * r.Denominator == BigInteger.One)
                {
                    rt = selector(r.Numerator, r.Denominator);

                    if (rt[0].Denominator == BigInteger.One && rt[1].Denominator == BigInteger.One)
                    {
                        list.Add(new[] { rt[0].Numerator, rt[1].Numerator });
                        break;
                    }
                }
            }
            foreach (var r in cf.Take(1 + cf.SubQuotients.Count + ((cf.PeriodicLength % 2 == 0) ? 1 : 2) * times * cf.PeriodicLength))
            {
                if (a * r.Numerator * r.Numerator + b * r.Numerator * r.Denominator + c * r.Denominator * r.Denominator == BigInteger.One)
                {
                    rt = selector(-r.Numerator, -r.Denominator);
                    if (rt[0].Denominator == BigInteger.One && rt[1].Denominator == BigInteger.One)
                    {
                        list.Add(new[] { rt[0].Numerator, rt[1].Numerator });
                        break;
                    }
                }
            }
            cf = new ContinuedFraction(s, b, -a2);
            foreach (var r in cf.Take(1 + cf.SubQuotients.Count + ((cf.PeriodicLength % 2 == 0) ? 1 : 2) * times * cf.PeriodicLength))
            {
                if (a * r.Numerator * r.Numerator + b * r.Numerator * r.Denominator + c * r.Denominator * r.Denominator == BigInteger.One)
                {
                    rt = selector(r.Numerator, r.Denominator);
                    if (rt[0].Denominator == BigInteger.One && rt[1].Denominator == BigInteger.One)
                    {
                        list.Add(new[] { rt[0].Numerator, rt[1].Numerator });
                        break;
                    }
                }
            }
            foreach (var r in cf.Take(1 + cf.SubQuotients.Count + ((cf.PeriodicLength % 2 == 0) ? 1 : 2) * times * cf.PeriodicLength))
            {
                if (a * r.Numerator * r.Numerator + b * r.Numerator * r.Denominator + c * r.Denominator * r.Denominator == BigInteger.One)
                {
                    rt = selector(-r.Numerator, -r.Denominator);
                    if (rt[0].Denominator == BigInteger.One && rt[1].Denominator == BigInteger.One)
                    {
                        list.Add(new[] { rt[0].Numerator, rt[1].Numerator });
                        break;
                    }
                }
            }
            return(list);
        }