// 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); }
public VeryLong DivideBy(VeryLong divisor, int numberOfDigits) { int numberDecimalPlacesDivisor = divisor.GetDecimalPlaces(); divisor = RemoveDecimalPlaces(divisor); FillUpDecimalPlacesWithZero(numberOfDigits + numberDecimalPlacesDivisor); string dividendString = ""; string divisorString = divisor.ToString(); string quotientDigit = ""; string quotient = ""; string mult = ""; string diff = ""; for (int i = 0; i < Length(); i++) { if (GetFirstDigit(i) == ".") { quotient += "."; continue; } dividendString += GetFirstDigit(i); dividendString = VeryLong.RemoveLeadingZeros(dividendString); quotientDigit = DivideSimple(dividendString, divisorString); quotient += quotientDigit; mult = (new VeryLong(quotientDigit).Multiply(new VeryLong(divisorString))).ToString(); diff = (new VeryLong(dividendString).Subtract(new VeryLong(mult))).ToString(); dividendString = diff; } VeryLong quotientResult = new VeryLong(quotient); int resultDecimalPlaces = quotientResult.GetDecimalPlaces(); quotientResult = RemoveDecimalPlaces(quotientResult); quotientResult.SetDecimalPlaces(resultDecimalPlaces - numberDecimalPlacesDivisor); quotientResult.RemoveLeadingZeros(); quotientResult.LimitPrecisionTo(numberOfDigits); return(quotientResult); }
// 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); }