Пример #1
0
        public BigFloat Calculate(int precision, Func <bool> stopFunc)
        {
            BigInteger l = 13591409;
            BigInteger x = 1;

            var goal = AccuracyGoal.Relative(precision + 10);

            BigFloat.DefaultAccuracyGoal = goal;
            BigFloat.DefaultRoundingMode = RoundingMode.TowardsNearest;

            var  m = (BigFloat)1;
            long k = 6;

            var sum = (BigFloat)13591409;

            for (var index = 1; index < int.MaxValue; index++)
            {
                //if (stopFunc())
                //{
                //    return -1;
                //}

                l += 545140134;
                x *= -262537412640768000;

                m = m * (BigInteger.Pow(k, 3) - 16 * k) /
                    BigInteger.Pow(index, 3);

                BigFloat c;
                sum += (c = (m * l / x));

                k += 12;

                var correctionDigits = -c.GetDecimalDigits();

                if (correctionDigits >= precision)
                {
                    break;
                }
            }

            var alpha = 426880 * BigFloat.Sqrt(10005, goal, RoundingMode.TowardsNearest);

            var pi = alpha / sum;

            //if (stopFunc())
            //{
            //    return -1;
            //}

            return(pi.RestrictPrecision(AccuracyGoal.Absolute(precision), new RoundingMode()));
        }
Пример #2
0
        public void CalculateHundedThousandDigitsOfPi(int precision)
        {
            // Arrange
            var picalcService = new PiCalcService();
            var expectedPi    = BigFloat.GetPi(
                AccuracyGoal.Absolute(precision),
                new RoundingMode());
            // Act
            var pi = picalcService.Calculate(precision, () => false);

            // Assert
            pi.Should().Be(expectedPi);
        }
Пример #3
0
 private void GetKFactor()
 {
     kFactor = BigFloat.Divide(kFactorRational.Numerator, kFactorRational.Denominator, AccuracyGoal.Absolute(kFactorRational.Numerator.BitCount + 10), RoundingMode.TowardsNegativeInfinity);
 }
Пример #4
0
        public static void WriteToTxt(List <BalancedNumber> numbers, bool extended)
        {
            StreamWriter writer = new StreamWriter(File.Open(basePath + (extended ? "Balanced Numbers Extended.txt" : "Balanced Numbers.txt"), FileMode.OpenOrCreate));
            int          i      = 0;

            foreach (BalancedNumber bn in numbers)
            {
                writer.WriteLine("Balanced number #{0}", i + 1);
                writer.WriteLine("\tNumber:          {0}", bn.number.ToString());
                if (extended)
                {
                    writer.WriteLine("\t  Digit Count:   {0}", GetDigits(bn.number).ToString());
                    WritePrimeFactors(bn.PrimeFactors.number, writer);
                }


                writer.WriteLine("\tSideNum:         {0}", bn.sideSum.ToString());
                if (extended)
                {
                    writer.WriteLine("\t  Digit Count:   {0}", GetDigits(bn.sideSum).ToString());
                    WritePrimeFactors(bn.PrimeFactors.sideSum, writer);
                }
                writer.WriteLine("\tK:               {0}", bn.k.ToString());
                if (extended)
                {
                    writer.WriteLine("\t  Digit Count:   {0}", GetDigits(bn.k).ToString());
                    WritePrimeFactors(bn.PrimeFactors.k, writer);
                }
                writer.WriteLine("\tKfactor:         {0}", bn.KFactor.ToString());
                if (extended)
                {
                    TestRational(bn.k, bn.PrimeFactors.kFactorNumerator, bn.number, bn.PrimeFactors.kFactorDenominator, writer);
                }
                if (i < numbers.Count - 1)
                {
                    BigFloat floatn   = bn.number;
                    BigFloat quoitent = BigFloat.Divide(numbers[i + 1].number, floatn, AccuracyGoal.Absolute(floatn.BinaryPrecision), RoundingMode.TowardsNearest);
                    writer.WriteLine("\tnFactor to next: {0}", quoitent.ToString());
                    writer.WriteLine();
                }
                else
                {
                    writer.WriteLine("\tnFactor to next: N/A\n");
                }
                i++;
            }
            writer.Flush();
        }
Пример #5
0
 public void TestExtremeMathPi(int precision)
 {
     var pi = BigFloat.GetPi(AccuracyGoal.Absolute(precision));
 }