Exemple #1
0
        public static VeryLong GetPiTermDivisorEuler(int n)
        {
            // 1 --> 2*3*4
            // 2 --> 4*5*6
            // 3 --> 6*7*8

            VeryLong factor1 = new VeryLong("2").Multiply(new VeryLong(n.ToString()));
            VeryLong factor2 = new VeryLong(factor1.Add(new VeryLong("1")).ToString());
            VeryLong factor3 = new VeryLong(factor2.Add(new VeryLong("1")).ToString());

            return(factor1.Multiply(factor2).Multiply(factor3));
        }
Exemple #2
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);
        }
Exemple #3
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();
        }