protected static int CompareUnsigned(HugeInteger a, HugeInteger b) { if (a.Length == b.Length) { return(String.Compare(a.ToString().TrimStart('-'), b.ToString().TrimStart('-'))); } return((a.Length > b.Length) ? 1 : -1); }
private static (HugeInteger, string) DivideBySub(HugeInteger initial, HugeInteger divided) { int q = 0; ulong a, b = 0; if (ulong.TryParse(initial.ToString(), out a) && ulong.TryParse(divided.ToString(), out b)) { return(new HugeInteger((a % b).ToString()), (a / b).ToString()); } HugeInteger d = new HugeInteger(divided.number, -divided.sign, true); while (initial >= divided) { initial += d; q++; } return(initial, q.ToString()); }
private static (string, string) Divide(HugeInteger initial, HugeInteger divided) { int sign = initial.sign * divided.sign; initial.sign = 1; divided.sign = 1; if (initial < divided) { return("0", initial.ToString()); } if (divided.isZero()) { throw new DivideByZeroException(); } if (divided.ToString() == "1") { return(((sign == -1) ? "-" : "") + initial.ToString(), "0"); } int zeros = Math.Min(divided.Length - divided.ToString().TrimEnd('0').Length, initial.Length - initial.ToString().TrimEnd('0').Length); divided = new HugeInteger(divided.ToString().Substring(0, divided.Length - zeros)); initial = new HugeInteger(initial.ToString().Substring(0, initial.Length - zeros)); HugeInteger newNumber; string dividedNumberStr = divided.ToString(); string q, tmp, sliceI, tail; q = tmp = tail = sliceI = ""; bool first = true; int i, count; int lastLength = divided.Length; while (initial >= divided) { i = initial.maxLength - initial.Length; sliceI = String.Join("", initial.number.Skip(i).Take(lastLength + ((first) ? 0 : 1))); count = (first) ? 1 : 2; while (HugeInteger.CompareUnsigned(sliceI, dividedNumberStr) < 0) { if (!first) { q += "0"; } sliceI = String.Join("", initial.number.Skip(i).Take(lastLength + count)); count++; } first = false; (newNumber, tmp) = HugeInteger.DivideBySub(new HugeInteger(sliceI), new HugeInteger(dividedNumberStr)); q += tmp; lastLength = newNumber.Length; tail = String.Join("", initial.number.Skip(initial.maxLength - initial.Length + sliceI.Length).Take(initial.maxLength - (initial.maxLength - initial.Length + sliceI.Length))); initial = new HugeInteger(newNumber.ToString() + tail); } if (tail != "") { q = q.PadRight(q.Length + tail.Length, '0'); } return(((sign == -1) ? "-" : "") + q, initial.ToString()); }
private static (string, string) Divide(HugeInteger initial, HugeInteger divided) { int sign = initial.sign * divided.sign; initial.sign = 1; divided.sign = 1; if (initial < divided) { return("0", initial.ToString()); } if (divided.isZero()) { throw new DivideByZeroException(); } if (divided.ToString() == "1") { return(((sign == -1) ? "-" : "") + initial.ToString(), "0"); } int zeros = Math.Min(divided.Length - String.Join("", divided.number).TrimStart('0').Length, initial.Length - String.Join("", initial.number).TrimStart('0').Length); string dividedNumberStr = String.Join("", divided.number.Skip(zeros).Take(divided.Length - zeros).Reverse()).TrimStart('0'); string initialNumberStr = String.Join("", initial.number.Skip(zeros).Take(initial.Length - zeros).Reverse()).TrimStart('0'); HugeInteger newNumber; string q, tmp, tail; q = tmp = tail = ""; bool first = true; bool zero = true; string sliceI; int count; int lastLength = divided.Length; while (HugeInteger.CompareUnsigned(initialNumberStr, dividedNumberStr) >= 0) { count = (zero) ? 0 : 1; int sliceLength = lastLength + count; sliceI = String.Join("", initialNumberStr.Take(sliceLength)); count++; while (HugeInteger.CompareUnsigned(sliceI, dividedNumberStr) < 0) { if (!first) { q += "0"; } sliceI += initialNumberStr[sliceI.Length].ToString(); count++; } first = false; (newNumber, tmp) = HugeInteger.DivideBySub(new HugeInteger(sliceI), divided); zero = newNumber.isZero() ? true : false; q += tmp; lastLength = newNumber.Length; tail = String.Join("", initialNumberStr.Skip(sliceI.Length).Take(initial.Length - sliceI.Length)); initialNumberStr = ((!newNumber.isZero()) ? newNumber.ToString() : "") + tail; } if (tail != "") { q = q.PadRight(q.Length + tail.Length, '0'); } return(((sign == -1) ? "-" : "") + q, initial.ToString()); }