/// <summary> /// Schoolbook division helper function. /// </summary> /// <param name="n1"></param> /// <param name="n2"></param> /// <param name="Q">Quotient output value</param> /// <param name="R">Remainder output value</param> private static void Div_31(BigInt n1, BigInt n2, BigInt Q, BigInt R) { int digitsN1 = n1.GetMSD() + 1; int digitsN2 = n2.GetMSD() + 1; if ((digitsN1 > digitsN2)) { BigInt n1New = new BigInt(n2); n1New.DigitShiftSelfLeft(1); //If n1 >= n2 * 2^32 if (!LtInt(n1, n1New)) { n1New.sign = n1.sign; SubFast(n1New, n1, n1New); Div_32(n1New, n2, Q, R); //Q = (A - B*2^32)/B + 2^32 Q.Add2Pow32Self(); return; } } UInt32 q = 0; if (digitsN1 >= 2) { UInt64 q64 = ((((UInt64)n1.digitArray[digitsN1 - 1]) << 32) + n1.digitArray[digitsN1 - 2]) / (UInt64)n2.digitArray[digitsN2 - 1]; if (q64 > 0xfffffffful) { q = 0xffffffff; } else { q = (UInt32)q64; } } BigInt temp = Mul(n2, q); if (GtInt(temp, n1)) { temp.SubInternalBits(n2.digitArray); q--; if (GtInt(temp, n1)) { temp.SubInternalBits(n2.digitArray); q--; } } Q.Zero(); Q.digitArray[0] = q; R.Assign(n1); R.SubInternalBits(temp.digitArray); }