Exemple #1
0
        static void Main()
        {
            int                answer = 0;
            int                search = 10001;
            BigInteger         max    = 0;
            RootAsContFraction r;

            for (int a = 0; a < search; ++a)
            {
                if (Math.Sqrt(a) % 1 != 0)
                {
                    r = new RootAsContFraction(a);
                    r.print();
                    Console.WriteLine();
                    if (r.repeatingBlock.Count % 2 == 1)
                    {
                        ++answer;
                    }
                }
            }

            Console.WriteLine("Answer: " + answer);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Exemple #2
0
        // returns the minimal solution of x to the
        // equation x^2 + d*y^2 = 1
        // Makes use of knowledge of Pell's equation!
        // makes some assumptions about the input!
        static BigInteger getMinSol(int d)
        {
            // first we need to know the period of the continued fraction
            // of the square root of d
            RootAsContFraction r         = new RootAsContFraction(d);
            List <int>         repeating = r.getRepeatingBlock();
            Rational           t;
            int i = 1;

            if (repeating.Count % 2 == 0)
            {
                // we do something when the continued fraction has even period
                // when we have even period, then let p = the period and our
                // solution corresponds to the pth convergent
                i = repeating.Count;
                t = r.toRational(i);
                if (t.a * t.a - d * t.b * t.b == 1)
                {
                    return(t.a);
                }
            }
            else
            {
                // we do something else when it has odd period
                // When we have odd period, let p = the period and
                // the solution corresponds to the 2pth convergent
                i = repeating.Count * 2;
                t = r.toRational(i);
                if (t.a * t.a - d * t.b * t.b == 1)
                {
                    return(t.a);
                }
            }

            // brute force search all convergents of the square root
            // written as a continued fraction and check for an answer
            // to the pell equation. This is used as a last resort in the
            // case that the above doesn't properly find a solution... which it should...
            while (t.a * t.a - d * t.b * t.b != 1)
            {
                t = r.toRational(++i);
            }

            // debug, this should never be reached
            Console.WriteLine("Period: " + r.getRepeatingBlock().Count);
            Console.WriteLine("Convergent Index: " + i);

            return(t.a);
        }
Exemple #3
0
        static void Main()
        {
            BigInteger answer  = 0;
            int        search  = (int)Math.Pow(10, 2);
            int        search2 = (int)Math.Pow(10, 2);

            for (int a = 2; a <= search; ++a)
            {
                if (Math.Sqrt(a) % 1 != 0)
                {
                    RootAsContFraction  r    = new RootAsContFraction(a);
                    Rational            soln = r.getPellEquationSolution();
                    BigDecimalExpansion bde;
                    for (int i = 0; i < search2; ++i)
                    {
                        soln = r.getNextPellEquationSoln(soln);
                    }
                    bde = new BigDecimalExpansion(soln, 100);

                    List <int> digits          = bde.getDecimalExpansion();
                    BigInteger wholePart       = bde.getWholePart();
                    int        sizeOfWholePart = (int)Math.Floor(BigInteger.Log10(wholePart)) + 1;
                    answer += wholePart;
                    Console.WriteLine(bde.ToString());
                    for (int i = 0; i < (100 - sizeOfWholePart); ++i)
                    {
                        answer += digits[i];
                    }
                }
            }

            Console.WriteLine("Answer: " + answer);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }