Example #1
0
        // Berechne die quadratwurzel aus q (Heronverfahren)
        //
        //         1    /       q   \
        // xn+1 = --- * | xn + ---- |
        //         2    \       xn  /
        // Each iteration about doubles the correct decimal places!
        public static VeryLong Sqrt(VeryLong input, int numberOfDigits)
        {
            int      additionalCalcDigits  = 5;
            int      additionalLimitDigits = 0;
            VeryLong limit = new VeryLong("0." + VeryLong.GetZeros(numberOfDigits + additionalLimitDigits) + "1"
                                          + VeryLong.GetZeros(additionalCalcDigits - additionalLimitDigits - 1));
            VeryLong xnew;
            VeryLong delta;
            VeryLong xn = new VeryLong(input.ToString());
            int      i  = 0;

            while (true)
            {
                VeryLong summand2 = input.DivideBy(xn, numberOfDigits + additionalCalcDigits);
                VeryLong factor   = xn.Add(summand2);
                xnew  = new VeryLong("0.5").Multiply(factor);
                delta = xnew.Subtract(xn);
                Console.WriteLine("i " + i);
                Console.WriteLine("Sqrt delta: " + delta.ToString());
                Console.WriteLine("limit:      " + limit.ToString());
                Console.WriteLine("Sqrt:       " + xnew.ToString());
                xn = xnew;
                i++;
                if (!delta.Abs().IsLargerOrEqual(limit))
                {
                    break;
                }
            }

            xn.LimitPrecisionTo(numberOfDigits);
            return(xn);
        }
Example #2
0
        //             4         4         4
        // pi = 3 + ------- - ------- + ------- +- ...
        //           2*3*4     4*5*6     6*7*8
        public static VeryLong GetPiTermEuler(int n, int numberOfDigits)
        {
            VeryLong four     = new VeryLong("4");
            VeryLong quotient = four.DivideBy(GetPiTermDivisorEuler(n), numberOfDigits);

            return(quotient);
        }
Example #3
0
        //  1      /  4       2        1        1    \
        //------ * |----- - ------ - ------ - ------ |
        // 16^n    \ 8n+1    8n+4     8n+5     8n+6  /
        public static VeryLong GetPiTermBBP(int n, int numberOfDigits, ref VeryLong factor)
        {
            VeryLong factor16 = new VeryLong(16.ToString());

            factor = factor.DivideBy(factor16, numberOfDigits);

            VeryLong summand1 = new VeryLong(4.ToString());
            VeryLong divisor1 = new VeryLong(8.ToString());

            divisor1 = divisor1.Multiply(new VeryLong(n.ToString())).Add(new VeryLong(1.ToString()));
            summand1 = summand1.DivideBy(divisor1, numberOfDigits);

            VeryLong subtrahend2 = new VeryLong(2.ToString());
            VeryLong divisor2    = new VeryLong(8.ToString());

            divisor2    = divisor2.Multiply(new VeryLong(n.ToString())).Add(new VeryLong(4.ToString()));
            subtrahend2 = subtrahend2.DivideBy(divisor2, numberOfDigits);

            VeryLong subtrahend3 = new VeryLong(1.ToString());
            VeryLong divisor3    = new VeryLong(8.ToString());

            divisor3    = divisor3.Multiply(new VeryLong(n.ToString())).Add(new VeryLong(5.ToString()));
            subtrahend3 = subtrahend3.DivideBy(divisor3, numberOfDigits);

            VeryLong subtrahend4 = new VeryLong(1.ToString());
            VeryLong divisor4    = new VeryLong(8.ToString());

            divisor4    = divisor4.Multiply(new VeryLong(n.ToString())).Add(new VeryLong(6.ToString()));
            subtrahend4 = subtrahend4.DivideBy(divisor4, numberOfDigits);

            VeryLong sum  = summand1.Subtract(subtrahend2).Subtract(subtrahend3).Subtract(subtrahend4);
            VeryLong term = sum.Multiply(factor);

            term.LimitPrecisionTo(numberOfDigits);

            return(term);
        }
Example #4
0
        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Enter a calculation like '1+2' or enter 'pi'. Operations are + - * /. Numbers are integers of any size.");
                string input = Console.ReadLine();
                if (input == "pi")
                {
                    Console.WriteLine("Enter the number of digits you want.");
                    string digitsString = Console.ReadLine();
                    int    numberDigits = 0;
                    if (int.TryParse(digitsString, out numberDigits))
                    {
                        string pi = VeryLongMath.Pi(numberDigits).ToString();
                        Console.WriteLine("Pi is: " + pi.ToString());
                    }
                    else
                    {
                        Console.WriteLine("invalid number.");
                    }

                    continue;
                }

                if (input == "sqrt")
                {
                    int      numberDigits;
                    VeryLong number;
                    Console.WriteLine("Square root of?");
                    string numberString = Console.ReadLine();
                    Console.WriteLine("number of digits?");
                    string digitsString = Console.ReadLine();
                    if (int.TryParse(digitsString, out numberDigits))
                    {
                        number = new VeryLong(numberString);
                        string sqrt = VeryLongMath.Sqrt(number, numberDigits).ToString();
                        Console.WriteLine("Sqrt of " + numberString + " is: " + sqrt.ToString());
                    }
                    else
                    {
                        Console.WriteLine("invalid number.");
                    }
                }

                string[] parts = input.Split(new char[] { '+', '-', '*', '/' });
                if (parts.Length != 2)
                {
                    Console.WriteLine("invalid input.");
                    continue;
                }

                VeryLong number0 = new VeryLong(parts[0]);
                VeryLong number1 = new VeryLong(parts[1]);

                if (input.Contains('+'))
                {
                    Console.WriteLine(number0.Add(number1).ToString());
                }
                else if (input.Contains('*'))
                {
                    Console.WriteLine(number0.Multiply(number1).ToString());
                }
                else if (input.Contains('-'))
                {
                    Console.WriteLine(number0.Subtract(number1).ToString());
                }
                else if (input.Contains('/'))
                {
                    VeryLong remainder;
                    VeryLong quotient = number0.DivideBy(number1, out remainder);
                    Console.WriteLine(quotient.ToString() + " Remainder: " + remainder);

                    VeryLong quotientWithDecimal = number0.DivideBy(number1, 100);
                    Console.WriteLine(quotientWithDecimal.ToString());
                }
                else
                {
                    Console.WriteLine("Please enter a valid operation...");
                }
            }

            Console.ReadLine();
        }