Длинная арифметика. Реализовать деление и умножение целочисленных чисел - C#
Формулировка задачи:
Добрый день. Нужно реализовать деление и умножение целочисленных чисел
Читал http://e-maxx.ru/algo/big_integer , деление длинного на длинное тут нет.
Вот попробовал его реализовать не очень то и получилось
С умножением тоже видел пару статей но не разобрался\не то что надо. В общем прошу вашей помощи
class Program { static void Main(string[] args) { Console.WriteLine("Операция длинного деления "); Console.WriteLine("Введите 1ое число"); string a = Console.ReadLine(); Console.WriteLine("Введите 2ое число"); string b = Console.ReadLine(); div(a, b); } static void div(string a, string b) { string a1, c = "", b1; int i, j; decimal buf; if (a.Length < b.Length) { Console.WriteLine("0"); return; } do { a1 = ""; b1 = ""; for (i = 0; i < b.Length; i++) { a1 += a[i]; } if (a1.Length < b.Length) { a1 += a[i]; } for (j = 9; j != 0; j--) { if (j * Convert.ToDecimal(b) <= Convert.ToDecimal(a1)) { b1 += j.ToString(); break; } } c += b1; buf = Convert.ToDecimal(a1) - (Convert.ToDecimal(b1) * Convert.ToDecimal(b)); a = (buf > 0 ? buf.ToString() : "").ToString() + a.Substring(a1.Length, a.Length - a1.Length); Console.WriteLine("buf {0} a {1}", buf, a); if (a.Length > 0) if (a[0] == '0') c += a; if (buf == 0 && a!= "") for (decimal o = decimal.Parse(a); i < a.Length; o++) c += "0"; } while (a.Length >= b.Length && a[0] >= b[0]); Console.WriteLine(c); } }
Решение задачи: «Длинная арифметика. Реализовать деление и умножение целочисленных чисел»
textual
Листинг программы
using System; using System.Diagnostics; using System.Globalization; using System.Runtime; [__DynamicallyInvokable] [Serializable] public struct BigInteger : IFormattable, IComparable, IComparable<BigInteger>, IEquatable<BigInteger> { private static readonly BigInteger s_bnMinInt = new BigInteger(-1, new uint[1] { (uint) int.MinValue }); private static readonly BigInteger s_bnOneInt = new BigInteger(1); private static readonly BigInteger s_bnZeroInt = new BigInteger(0); private static readonly BigInteger s_bnMinusOneInt = new BigInteger(-1); internal int _sign; internal uint[] _bits; private const int knMaskHighBit = -2147483648; private const uint kuMaskHighBit = 2147483648U; private const int kcbitUint = 32; private const int kcbitUlong = 64; private const int DecimalScaleFactorMask = 16711680; private const int DecimalSignMask = -2147483648; [__DynamicallyInvokable] public static BigInteger Zero { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return BigInteger.s_bnZeroInt; } } [__DynamicallyInvokable] public static BigInteger One { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return BigInteger.s_bnOneInt; } } [__DynamicallyInvokable] public static BigInteger MinusOne { [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return BigInteger.s_bnMinusOneInt; } } [__DynamicallyInvokable] public bool IsPowerOfTwo { [__DynamicallyInvokable] get { if (this._bits == null) { if ((this._sign & this._sign - 1) == 0) return this._sign != 0; else return false; } else { if (this._sign != 1) return false; int index = BigInteger.Length(this._bits) - 1; if (((int) this._bits[index] & (int) this._bits[index] - 1) != 0) return false; while (--index >= 0) { if ((int) this._bits[index] != 0) return false; } return true; } } } [__DynamicallyInvokable] public bool IsZero { [__DynamicallyInvokable] get { return this._sign == 0; } } [__DynamicallyInvokable] public bool IsOne { [__DynamicallyInvokable] get { if (this._sign == 1) return this._bits == null; else return false; } } [__DynamicallyInvokable] public bool IsEven { [__DynamicallyInvokable] get { if (this._bits != null) return ((int) this._bits[0] & 1) == 0; else return (this._sign & 1) == 0; } } [__DynamicallyInvokable] public int Sign { [__DynamicallyInvokable] get { return (this._sign >> 31) - (-this._sign >> 31); } } internal int _Sign { get { return this._sign; } } internal uint[] _Bits { get { return this._bits; } } static BigInteger() { } [__DynamicallyInvokable] public BigInteger(int value) { if (value == int.MinValue) { this = BigInteger.s_bnMinInt; } else { this._sign = value; this._bits = (uint[]) null; } } [CLSCompliant(false)] [__DynamicallyInvokable] public BigInteger(uint value) { if (value <= (uint) int.MaxValue) { this._sign = (int) value; this._bits = (uint[]) null; } else { this._sign = 1; this._bits = new uint[1]; this._bits[0] = value; } } [__DynamicallyInvokable] public BigInteger(long value) { if ((long) int.MinValue <= value && value <= (long) int.MaxValue) { if (value == (long) int.MinValue) { this = BigInteger.s_bnMinInt; } else { this._sign = (int) value; this._bits = (uint[]) null; } } else { ulong num; if (value < 0L) { num = (ulong) -value; this._sign = -1; } else { num = (ulong) value; this._sign = 1; } this._bits = new uint[2]; this._bits[0] = (uint) num; this._bits[1] = (uint) (num >> 32); } } [CLSCompliant(false)] [__DynamicallyInvokable] public BigInteger(ulong value) { if (value <= (ulong) int.MaxValue) { this._sign = (int) value; this._bits = (uint[]) null; } else { this._sign = 1; this._bits = new uint[2]; this._bits[0] = (uint) value; this._bits[1] = (uint) (value >> 32); } } <exception cref="T:System.OverflowException">Значение параметра <paramref name="value"/> — <see cref="F:System.Single.NaN"/>. -или- Значение <paramref name="value"/> равно <see cref="F:System.Single.NegativeInfinity"/>. -или- Значение <paramref name="value"/> равно <see cref="F:System.Single.PositiveInfinity"/>.</exception> [__DynamicallyInvokable] public BigInteger(float value) { if (float.IsInfinity(value)) throw new OverflowException(SR.GetString("Overflow_BigIntInfinity")); if (float.IsNaN(value)) throw new OverflowException(SR.GetString("Overflow_NotANumber")); this._sign = 0; this._bits = (uint[]) null; this.SetBitsFromDouble((double) value); } <exception cref="T:System.OverflowException">Значение параметра <paramref name="value"/> — <see cref="F:System.Double.NaN"/>. -или- Значение <paramref name="value"/> равно <see cref="F:System.Double.NegativeInfinity"/>. -или- Значение <paramref name="value"/> равно <see cref="F:System.Double.PositiveInfinity"/>.</exception> [__DynamicallyInvokable] public BigInteger(double value) { if (double.IsInfinity(value)) throw new OverflowException(SR.GetString("Overflow_BigIntInfinity")); if (double.IsNaN(value)) throw new OverflowException(SR.GetString("Overflow_NotANumber")); this._sign = 0; this._bits = (uint[]) null; this.SetBitsFromDouble(value); } [__DynamicallyInvokable] public BigInteger(Decimal value) { int[] bits = Decimal.GetBits(Decimal.Truncate(value)); int length = 3; while (length > 0 && bits[length - 1] == 0) --length; if (length == 0) this = BigInteger.s_bnZeroInt; else if (length == 1 && bits[0] > 0) { this._sign = bits[0]; this._sign *= (bits[3] & int.MinValue) != 0 ? -1 : 1; this._bits = (uint[]) null; } else { this._bits = new uint[length]; this._bits[0] = (uint) bits[0]; if (length > 1) this._bits[1] = (uint) bits[1]; if (length > 2) this._bits[2] = (uint) bits[2]; this._sign = (bits[3] & int.MinValue) != 0 ? -1 : 1; } } <exception cref="T:System.ArgumentNullException">Параметр <paramref name="value"/> имеет значение null.</exception> [CLSCompliant(false)] [__DynamicallyInvokable] public BigInteger(byte[] value) { if (value == null) throw new ArgumentNullException("value"); int length1 = value.Length; bool flag1 = length1 > 0 && ((int) value[length1 - 1] & 128) == 128; while (length1 > 0 && (int) value[length1 - 1] == 0) --length1; if (length1 == 0) { this._sign = 0; this._bits = (uint[]) null; } else if (length1 <= 4) { this._sign = !flag1 ? 0 : -1; for (int index = length1 - 1; index >= 0; --index) { this._sign <<= 8; this._sign |= (int) value[index]; } this._bits = (uint[]) null; if (this._sign < 0 && !flag1) { this._bits = new uint[1]; this._bits[0] = (uint) this._sign; this._sign = 1; } if (this._sign != int.MinValue) return; this = BigInteger.s_bnMinInt; } else { int num = length1 % 4; int length2 = length1 / 4 + (num == 0 ? 0 : 1); bool flag2 = true; uint[] d = new uint[length2]; int index1 = 3; int index2; for (index2 = 0; index2 < length2 - (num == 0 ? 0 : 1); ++index2) { for (int index3 = 0; index3 < 4; ++index3) { if ((int) value[index1] != 0) flag2 = false; d[index2] <<= 8; d[index2] |= (uint) value[index1]; --index1; } index1 += 8; } if (num != 0) { if (flag1) d[length2 - 1] = uint.MaxValue; for (int index3 = length1 - 1; index3 >= length1 - num; --index3) { if ((int) value[index3] != 0) flag2 = false; d[index2] <<= 8; d[index2] |= (uint) value[index3]; } } if (flag2) this = BigInteger.s_bnZeroInt; else if (flag1) { NumericsHelpers.DangerousMakeTwosComplement(d); int length3 = d.Length; while (length3 > 0 && (int) d[length3 - 1] == 0) --length3; if (length3 == 1 && (int) d[0] > 0) { if ((int) d[0] == 1) this = BigInteger.s_bnMinusOneInt; else if ((int) d[0] == int.MinValue) { this = BigInteger.s_bnMinInt; } else { this._sign = -1 * (int) d[0]; this._bits = (uint[]) null; } } else if (length3 != d.Length) { this._sign = -1; this._bits = new uint[length3]; Array.Copy((Array) d, (Array) this._bits, length3); } else { this._sign = -1; this._bits = d; } } else { this._sign = 1; this._bits = d; } } } internal BigInteger(int n, uint[] rgu) { this._sign = n; this._bits = rgu; } internal BigInteger(uint[] value, bool negative) { if (value == null) throw new ArgumentNullException("value"); int length = value.Length; while (length > 0 && (int) value[length - 1] == 0) --length; if (length == 0) this = BigInteger.s_bnZeroInt; else if (length == 1 && value[0] < (uint) int.MinValue) { this._sign = negative ? -(int) value[0] : (int) value[0]; this._bits = (uint[]) null; if (this._sign != int.MinValue) return; this = BigInteger.s_bnMinInt; } else { this._sign = negative ? -1 : 1; this._bits = new uint[length]; Array.Copy((Array) value, (Array) this._bits, length); } } private BigInteger(uint[] value) { if (value == null) throw new ArgumentNullException("value"); int length1 = value.Length; bool flag = length1 > 0 && ((int) value[length1 - 1] & int.MinValue) == int.MinValue; while (length1 > 0 && (int) value[length1 - 1] == 0) --length1; if (length1 == 0) this = BigInteger.s_bnZeroInt; else if (length1 == 1) { if ((int) value[0] < 0 && !flag) { this._bits = new uint[1]; this._bits[0] = value[0]; this._sign = 1; } else if (int.MinValue == (int) value[0]) { this = BigInteger.s_bnMinInt; } else { this._sign = (int) value[0]; this._bits = (uint[]) null; } } else if (!flag) { if (length1 != value.Length) { this._sign = 1; this._bits = new uint[length1]; Array.Copy((Array) value, (Array) this._bits, length1); } else { this._sign = 1; this._bits = value; } } else { NumericsHelpers.DangerousMakeTwosComplement(value); int length2 = value.Length; while (length2 > 0 && (int) value[length2 - 1] == 0) --length2; if (length2 == 1 && (int) value[0] > 0) { if ((int) value[0] == 1) this = BigInteger.s_bnMinusOneInt; else if ((int) value[0] == int.MinValue) { this = BigInteger.s_bnMinInt; } else { this._sign = -1 * (int) value[0]; this._bits = (uint[]) null; } } else if (length2 != value.Length) { this._sign = -1; this._bits = new uint[length2]; Array.Copy((Array) value, (Array) this._bits, length2); } else { this._sign = -1; this._bits = value; } } } [__DynamicallyInvokable] public static implicit operator BigInteger(byte value) { return new BigInteger((int) value); } [CLSCompliant(false)] [__DynamicallyInvokable] public static implicit operator BigInteger(sbyte value) { return new BigInteger((int) value); } [__DynamicallyInvokable] public static implicit operator BigInteger(short value) { return new BigInteger((int) value); } [CLSCompliant(false)] [__DynamicallyInvokable] public static implicit operator BigInteger(ushort value) { return new BigInteger((int) value); } [__DynamicallyInvokable] public static implicit operator BigInteger(int value) { return new BigInteger(value); } [CLSCompliant(false)] [__DynamicallyInvokable] public static implicit operator BigInteger(uint value) { return new BigInteger(value); } [__DynamicallyInvokable] public static implicit operator BigInteger(long value) { return new BigInteger(value); } [CLSCompliant(false)] [__DynamicallyInvokable] public static implicit operator BigInteger(ulong value) { return new BigInteger(value); } <exception cref="T:System.OverflowException">Параметр <paramref name="value"/> имеет значение <see cref="F:System.Single.NaN"/>. -или- Параметр <paramref name="value"/> имеет значение <see cref="F:System.Single.PositiveInfinity"/>. -или- Параметр <paramref name="value"/> имеет значение <see cref="F:System.Single.NegativeInfinity"/>.</exception> [__DynamicallyInvokable] public static explicit operator BigInteger(float value) { return new BigInteger(value); } <exception cref="T:System.OverflowException">Параметр <paramref name="value"/> имеет значение <see cref="F:System.Double.NaN"/>. -или- Параметр <paramref name="value"/> имеет значение <see cref="F:System.Double.PositiveInfinity"/>. -или- Параметр <paramref name="value"/> имеет значение <see cref="F:System.Double.NegativeInfinity"/>.</exception> [__DynamicallyInvokable] public static explicit operator BigInteger(double value) { return new BigInteger(value); } [__DynamicallyInvokable] public static explicit operator BigInteger(Decimal value) { return new BigInteger(value); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.Byte.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.Byte.MaxValue"/>.</exception> [__DynamicallyInvokable] public static explicit operator byte(BigInteger value) { return checked ((byte) (int) value); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.SByte.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.SByte.MaxValue"/>.</exception> [CLSCompliant(false)] [__DynamicallyInvokable] public static explicit operator sbyte(BigInteger value) { return checked ((sbyte) (int) value); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.Int16.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.Int16.MaxValue"/>.</exception> [__DynamicallyInvokable] public static explicit operator short(BigInteger value) { return checked ((short) (int) value); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.UInt16.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.UInt16.MaxValue"/>.</exception> [CLSCompliant(false)] [__DynamicallyInvokable] public static explicit operator ushort(BigInteger value) { return checked ((ushort) (int) value); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.Int32.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.Int32.MaxValue"/>.</exception> [__DynamicallyInvokable] public static explicit operator int(BigInteger value) { if (value._bits == null) return value._sign; if (BigInteger.Length(value._bits) > 1) throw new OverflowException(SR.GetString("Overflow_Int32")); if (value._sign > 0) return checked ((int) value._bits[0]); if (value._bits[0] > (uint) int.MinValue) throw new OverflowException(SR.GetString("Overflow_Int32")); else return -(int) value._bits[0]; } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.UInt32.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.UInt32.MaxValue"/>.</exception> [CLSCompliant(false)] [__DynamicallyInvokable] public static explicit operator uint(BigInteger value) { if (value._bits == null) return checked ((uint) value._sign); if (BigInteger.Length(value._bits) > 1 || value._sign < 0) throw new OverflowException(SR.GetString("Overflow_UInt32")); else return value._bits[0]; } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.Int64.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.Int64.MaxValue"/>.</exception> [__DynamicallyInvokable] public static explicit operator long(BigInteger value) { if (value._bits == null) return (long) value._sign; int num1 = BigInteger.Length(value._bits); if (num1 > 2) throw new OverflowException(SR.GetString("Overflow_Int64")); ulong num2 = num1 <= 1 ? (ulong) value._bits[0] : NumericsHelpers.MakeUlong(value._bits[1], value._bits[0]); long num3 = value._sign > 0 ? (long) num2 : -(long) num2; if (num3 > 0L && value._sign > 0 || num3 < 0L && value._sign < 0) return num3; else throw new OverflowException(SR.GetString("Overflow_Int64")); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.UInt64.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.UInt64.MaxValue"/>.</exception> [CLSCompliant(false)] [__DynamicallyInvokable] public static explicit operator ulong(BigInteger value) { if (value._bits == null) return checked ((ulong) value._sign); int num = BigInteger.Length(value._bits); if (num > 2 || value._sign < 0) throw new OverflowException(SR.GetString("Overflow_UInt64")); if (num > 1) return NumericsHelpers.MakeUlong(value._bits[1], value._bits[0]); else return (ulong) value._bits[0]; } [__DynamicallyInvokable] public static explicit operator float(BigInteger value) { return (float) (double) value; } [__DynamicallyInvokable] public static explicit operator double(BigInteger value) { if (value._bits == null) return (double) value._sign; int sign = 1; int exp; ulong man; new BigIntegerBuilder(value, ref sign).GetApproxParts(out exp, out man); return NumericsHelpers.GetDoubleFromParts(sign, exp, man); } <exception cref="T:System.OverflowException">Значение свойства <paramref name="value"/> меньше значения <see cref="F:System.Decimal.MinValue"/>. -или- Значение <paramref name="value"/> больше значения <see cref="F:System.Decimal.MaxValue"/>.</exception> [__DynamicallyInvokable] public static explicit operator Decimal(BigInteger value) { if (value._bits == null) return (Decimal) value._sign; int num = BigInteger.Length(value._bits); if (num > 3) throw new OverflowException(SR.GetString("Overflow_Decimal")); int lo = 0; int mid = 0; int hi = 0; if (num > 2) hi = (int) value._bits[2]; if (num > 1) mid = (int) value._bits[1]; if (num > 0) lo = (int) value._bits[0]; return new Decimal(lo, mid, hi, value._sign < 0, (byte) 0); } [__DynamicallyInvokable] public static BigInteger operator &(BigInteger left, BigInteger right) { if (left.IsZero || right.IsZero) return BigInteger.Zero; uint[] numArray1 = left.ToUInt32Array(); uint[] numArray2 = right.ToUInt32Array(); uint[] numArray3 = new uint[Math.Max(numArray1.Length, numArray2.Length)]; uint num1 = left._sign < 0 ? uint.MaxValue : 0U; uint num2 = right._sign < 0 ? uint.MaxValue : 0U; for (int index = 0; index < numArray3.Length; ++index) { uint num3 = index < numArray1.Length ? numArray1[index] : num1; uint num4 = index < numArray2.Length ? numArray2[index] : num2; numArray3[index] = num3 & num4; } return new BigInteger(numArray3); } [__DynamicallyInvokable] public static BigInteger operator |(BigInteger left, BigInteger right) { if (left.IsZero) return right; if (right.IsZero) return left; uint[] numArray1 = left.ToUInt32Array(); uint[] numArray2 = right.ToUInt32Array(); uint[] numArray3 = new uint[Math.Max(numArray1.Length, numArray2.Length)]; uint num1 = left._sign < 0 ? uint.MaxValue : 0U; uint num2 = right._sign < 0 ? uint.MaxValue : 0U; for (int index = 0; index < numArray3.Length; ++index) { uint num3 = index < numArray1.Length ? numArray1[index] : num1; uint num4 = index < numArray2.Length ? numArray2[index] : num2; numArray3[index] = num3 | num4; } return new BigInteger(numArray3); } [__DynamicallyInvokable] public static BigInteger operator ^(BigInteger left, BigInteger right) { uint[] numArray1 = left.ToUInt32Array(); uint[] numArray2 = right.ToUInt32Array(); uint[] numArray3 = new uint[Math.Max(numArray1.Length, numArray2.Length)]; uint num1 = left._sign < 0 ? uint.MaxValue : 0U; uint num2 = right._sign < 0 ? uint.MaxValue : 0U; for (int index = 0; index < numArray3.Length; ++index) { uint num3 = index < numArray1.Length ? numArray1[index] : num1; uint num4 = index < numArray2.Length ? numArray2[index] : num2; numArray3[index] = num3 ^ num4; } return new BigInteger(numArray3); } [__DynamicallyInvokable] public static BigInteger operator <<(BigInteger value, int shift) { if (shift == 0) return value; if (shift == int.MinValue) return value >> int.MaxValue >> 1; if (shift < 0) return value >> -shift; int num1 = shift / 32; int num2 = shift - num1 * 32; uint[] xd; int xl; bool forBitManipulation = BigInteger.GetPartsForBitManipulation(ref value, out xd, out xl); uint[] numArray = new uint[xl + num1 + 1]; if (num2 == 0) { for (int index = 0; index < xl; ++index) numArray[index + num1] = xd[index]; } else { int num3 = 32 - num2; uint num4 = 0U; int index; for (index = 0; index < xl; ++index) { uint num5 = xd[index]; numArray[index + num1] = num5 << num2 | num4; num4 = num5 >> num3; } numArray[index + num1] = num4; } return new BigInteger(numArray, forBitManipulation); } [__DynamicallyInvokable] public static BigInteger operator >>(BigInteger value, int shift) { if (shift == 0) return value; if (shift == int.MinValue) return value << int.MaxValue << 1; if (shift < 0) return value << -shift; int num1 = shift / 32; int num2 = shift - num1 * 32; uint[] xd; int xl; bool forBitManipulation = BigInteger.GetPartsForBitManipulation(ref value, out xd, out xl); if (forBitManipulation) { if (shift >= 32 * xl) return BigInteger.MinusOne; uint[] numArray = new uint[xl]; Array.Copy((Array) xd, (Array) numArray, xl); xd = numArray; NumericsHelpers.DangerousMakeTwosComplement(xd); } int length = xl - num1; if (length < 0) length = 0; uint[] d = new uint[length]; if (num2 == 0) { for (int index = xl - 1; index >= num1; --index) d[index - num1] = xd[index]; } else { int num3 = 32 - num2; uint num4 = 0U; for (int index = xl - 1; index >= num1; --index) { uint num5 = xd[index]; d[index - num1] = !forBitManipulation || index != xl - 1 ? num5 >> num2 | num4 : num5 >> num2 | (uint) (-1 << num3); num4 = num5 << num3; } } if (forBitManipulation) NumericsHelpers.DangerousMakeTwosComplement(d); return new BigInteger(d, forBitManipulation); } [__DynamicallyInvokable] public static BigInteger operator ~(BigInteger value) { return -(value + BigInteger.One); } [__DynamicallyInvokable] public static BigInteger operator -(BigInteger value) { value._sign = -value._sign; return value; } [__DynamicallyInvokable] public static BigInteger operator +(BigInteger value) { return value; } [__DynamicallyInvokable] public static BigInteger operator ++(BigInteger value) { return value + BigInteger.One; } [__DynamicallyInvokable] public static BigInteger operator --(BigInteger value) { return value - BigInteger.One; } [__DynamicallyInvokable] public static BigInteger operator +(BigInteger left, BigInteger right) { if (right.IsZero) return left; if (left.IsZero) return right; int sign1 = 1; int sign2 = 1; BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(left, ref sign1); BigIntegerBuilder reg = new BigIntegerBuilder(right, ref sign2); if (sign1 == sign2) bigIntegerBuilder.Add(ref reg); else bigIntegerBuilder.Sub(ref sign1, ref reg); return bigIntegerBuilder.GetInteger(sign1); } [__DynamicallyInvokable] public static BigInteger operator -(BigInteger left, BigInteger right) { if (right.IsZero) return left; if (left.IsZero) return -right; int sign1 = 1; int sign2 = -1; BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(left, ref sign1); BigIntegerBuilder reg = new BigIntegerBuilder(right, ref sign2); if (sign1 == sign2) bigIntegerBuilder.Add(ref reg); else bigIntegerBuilder.Sub(ref sign1, ref reg); return bigIntegerBuilder.GetInteger(sign1); } [__DynamicallyInvokable] public static BigInteger operator *(BigInteger left, BigInteger right) { int sign = 1; BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(left, ref sign); BigIntegerBuilder regMul = new BigIntegerBuilder(right, ref sign); bigIntegerBuilder.Mul(ref regMul); return bigIntegerBuilder.GetInteger(sign); } <exception cref="T:System.DivideByZeroException">Значение параметра <paramref name="divisor"/> равно нулю (0).</exception> [__DynamicallyInvokable] public static BigInteger operator /(BigInteger dividend, BigInteger divisor) { int sign = 1; BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(dividend, ref sign); BigIntegerBuilder regDen = new BigIntegerBuilder(divisor, ref sign); bigIntegerBuilder.Div(ref regDen); return bigIntegerBuilder.GetInteger(sign); } <exception cref="T:System.DivideByZeroException">Значение параметра <paramref name="divisor"/> равно нулю (0).</exception> [__DynamicallyInvokable] public static BigInteger operator %(BigInteger dividend, BigInteger divisor) { int sign1 = 1; int sign2 = 1; BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(dividend, ref sign1); BigIntegerBuilder regDen = new BigIntegerBuilder(divisor, ref sign2); bigIntegerBuilder.Mod(ref regDen); return bigIntegerBuilder.GetInteger(sign1); } [__DynamicallyInvokable] public static bool operator <(BigInteger left, BigInteger right) { return left.CompareTo(right) < 0; } [__DynamicallyInvokable] public static bool operator <=(BigInteger left, BigInteger right) { return left.CompareTo(right) <= 0; } [__DynamicallyInvokable] public static bool operator >(BigInteger left, BigInteger right) { return left.CompareTo(right) > 0; } [__DynamicallyInvokable] public static bool operator >=(BigInteger left, BigInteger right) { return left.CompareTo(right) >= 0; } [__DynamicallyInvokable] public static bool operator ==(BigInteger left, BigInteger right) { return left.Equals(right); } [__DynamicallyInvokable] public static bool operator !=(BigInteger left, BigInteger right) { return !left.Equals(right); } [__DynamicallyInvokable] public static bool operator <(BigInteger left, long right) { return left.CompareTo(right) < 0; } [__DynamicallyInvokable] public static bool operator <=(BigInteger left, long right) { return left.CompareTo(right) <= 0; } [__DynamicallyInvokable] public static bool operator >(BigInteger left, long right) { return left.CompareTo(right) > 0; } [__DynamicallyInvokable] public static bool operator >=(BigInteger left, long right) { return left.CompareTo(right) >= 0; } [__DynamicallyInvokable] public static bool operator ==(BigInteger left, long right) { return left.Equals(right); } [__DynamicallyInvokable] public static bool operator !=(BigInteger left, long right) { return !left.Equals(right); } [__DynamicallyInvokable] public static bool operator <(long left, BigInteger right) { return right.CompareTo(left) > 0; } [__DynamicallyInvokable] public static bool operator <=(long left, BigInteger right) { return right.CompareTo(left) >= 0; } [__DynamicallyInvokable] public static bool operator >(long left, BigInteger right) { return right.CompareTo(left) < 0; } [__DynamicallyInvokable] public static bool operator >=(long left, BigInteger right) { return right.CompareTo(left) <= 0; } [__DynamicallyInvokable] public static bool operator ==(long left, BigInteger right) { return right.Equals(left); } [__DynamicallyInvokable] public static bool operator !=(long left, BigInteger right) { return !right.Equals(left); } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator <(BigInteger left, ulong right) { return left.CompareTo(right) < 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator <=(BigInteger left, ulong right) { return left.CompareTo(right) <= 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator >(BigInteger left, ulong right) { return left.CompareTo(right) > 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator >=(BigInteger left, ulong right) { return left.CompareTo(right) >= 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator ==(BigInteger left, ulong right) { return left.Equals(right); } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator !=(BigInteger left, ulong right) { return !left.Equals(right); } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator <(ulong left, BigInteger right) { return right.CompareTo(left) > 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator <=(ulong left, BigInteger right) { return right.CompareTo(left) >= 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator >(ulong left, BigInteger right) { return right.CompareTo(left) < 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator >=(ulong left, BigInteger right) { return right.CompareTo(left) <= 0; } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator ==(ulong left, BigInteger right) { return right.Equals(left); } [CLSCompliant(false)] [__DynamicallyInvokable] public static bool operator !=(ulong left, BigInteger right) { return !right.Equals(left); } [__DynamicallyInvokable] public override bool Equals(object obj) { if (!(obj is BigInteger)) return false; else return this.Equals((BigInteger) obj); } [__DynamicallyInvokable] public override int GetHashCode() { if (this._bits == null) return this._sign; int n1 = this._sign; int index = BigInteger.Length(this._bits); while (--index >= 0) n1 = NumericsHelpers.CombineHash(n1, (int) this._bits[index]); return n1; } [__DynamicallyInvokable] public bool Equals(long other) { if (this._bits == null) return (long) this._sign == other; int num1; if (((long) this._sign ^ other) < 0L || (num1 = BigInteger.Length(this._bits)) > 2) return false; ulong num2 = other < 0L ? (ulong) -other : (ulong) other; if (num1 == 1) return (long) this._bits[0] == (long) num2; else return (long) NumericsHelpers.MakeUlong(this._bits[1], this._bits[0]) == (long) num2; } [CLSCompliant(false)] [__DynamicallyInvokable] public bool Equals(ulong other) { if (this._sign < 0) return false; if (this._bits == null) return (long) this._sign == (long) other; int num = BigInteger.Length(this._bits); if (num > 2) return false; if (num == 1) return (long) this._bits[0] == (long) other; else return (long) NumericsHelpers.MakeUlong(this._bits[1], this._bits[0]) == (long) other; } [__DynamicallyInvokable] public bool Equals(BigInteger other) { if (this._sign != other._sign) return false; if (this._bits == other._bits) return true; if (this._bits == null || other._bits == null) return false; int cu = BigInteger.Length(this._bits); if (cu != BigInteger.Length(other._bits)) return false; else return BigInteger.GetDiffLength(this._bits, other._bits, cu) == 0; } [__DynamicallyInvokable] public int CompareTo(long other) { if (this._bits == null) return ((long) this._sign).CompareTo(other); int num1; if (((long) this._sign ^ other) < 0L || (num1 = BigInteger.Length(this._bits)) > 2) return this._sign; ulong num2 = other < 0L ? (ulong) -other : (ulong) other; return this._sign * (num1 == 2 ? NumericsHelpers.MakeUlong(this._bits[1], this._bits[0]) : (ulong) this._bits[0]).CompareTo(num2); } [CLSCompliant(false)] [__DynamicallyInvokable] public int CompareTo(ulong other) { if (this._sign < 0) return -1; if (this._bits == null) return ((ulong) this._sign).CompareTo(other); int num = BigInteger.Length(this._bits); if (num > 2) return 1; else return (num == 2 ? NumericsHelpers.MakeUlong(this._bits[1], this._bits[0]) : (ulong) this._bits[0]).CompareTo(other); } [__DynamicallyInvokable] public int CompareTo(BigInteger other) { if ((this._sign ^ other._sign) < 0) return this._sign >= 0 ? 1 : -1; else if (this._bits == null) { if (other._bits != null) return -other._sign; if (this._sign < other._sign) return -1; return this._sign <= other._sign ? 0 : 1; } else { int cu; int num; if (other._bits == null || (cu = BigInteger.Length(this._bits)) > (num = BigInteger.Length(other._bits))) return this._sign; if (cu < num) return -this._sign; int diffLength = BigInteger.GetDiffLength(this._bits, other._bits, cu); if (diffLength == 0) return 0; if (this._bits[diffLength - 1] >= other._bits[diffLength - 1]) return this._sign; else return -this._sign; } } <exception cref="T:System.ArgumentException"><paramref name="obj"/> не является объектом типа <see cref="T:System.Numerics.BigInteger"/>.</exception> public int CompareTo(object obj) { if (obj == null) return 1; if (!(obj is BigInteger)) throw new ArgumentException(SR.GetString("Argument_MustBeBigInt")); else return this.CompareTo((BigInteger) obj); } [__DynamicallyInvokable] public byte[] ToByteArray() { if (this._bits == null && this._sign == 0) return new byte[1]; uint[] d; byte num1; if (this._bits == null) { d = new uint[1] { (uint) this._sign }; num1 = this._sign < 0 ? byte.MaxValue : (byte) 0; } else if (this._sign == -1) { d = (uint[]) this._bits.Clone(); NumericsHelpers.DangerousMakeTwosComplement(d); num1 = byte.MaxValue; } else { d = this._bits; num1 = (byte) 0; } byte[] numArray1 = new byte[checked (4 * d.Length)]; int num2 = 0; for (int index1 = 0; index1 < d.Length; ++index1) { uint num3 = d[index1]; for (int index2 = 0; index2 < 4; ++index2) { numArray1[num2++] = (byte) (num3 & (uint) byte.MaxValue); num3 >>= 8; } } int index = numArray1.Length - 1; while (index > 0 && (int) numArray1[index] == (int) num1) --index; bool flag = ((int) numArray1[index] & 128) != ((int) num1 & 128); byte[] numArray2 = new byte[index + 1 + (flag ? 1 : 0)]; Array.Copy((Array) numArray1, (Array) numArray2, index + 1); if (flag) numArray2[numArray2.Length - 1] = num1; return numArray2; } [__DynamicallyInvokable] public override string ToString() { return BigNumber.FormatBigInteger(this, (string) null, NumberFormatInfo.CurrentInfo); } [__DynamicallyInvokable] public string ToString(IFormatProvider provider) { return BigNumber.FormatBigInteger(this, (string) null, NumberFormatInfo.GetInstance(provider)); } <exception cref="T:System.FormatException">Параметр <paramref name="format"/> не является допустимой строкой формата.</exception> [__DynamicallyInvokable] public string ToString(string format) { return BigNumber.FormatBigInteger(this, format, NumberFormatInfo.CurrentInfo); } <exception cref="T:System.FormatException">Параметр <paramref name="format"/> не является допустимой строкой формата.</exception> [__DynamicallyInvokable] public string ToString(string format, IFormatProvider provider) { return BigNumber.FormatBigInteger(this, format, NumberFormatInfo.GetInstance(provider)); } <exception cref="T:System.ArgumentNullException">Параметр <paramref name="value"/> имеет значение null.</exception><exception cref="T:System.FormatException">Формат параметра <paramref name="value"/> является неправильным.</exception> [__DynamicallyInvokable] public static BigInteger Parse(string value) { return BigNumber.ParseBigInteger(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } <exception cref="T:System.ArgumentException">Значение параметра <paramref name="style"/> не равно значению <see cref="T:System.Globalization.NumberStyles"/>. -или- <paramref name="style"/> включает флаги <see cref="F:System.Globalization.NumberStyles.AllowHexSpecifier"/> и <see cref="F:System.Globalization.NumberStyles.HexNumber"/> вместе с другим значениями.</exception><exception cref="T:System.ArgumentNullException">Параметр <paramref name="value"/> имеет значение null.</exception><exception cref="T:System.FormatException"><paramref name="value"/> не соответствует образцу ввода, заданному <see cref="T:System.Globalization.NumberStyles"/>.</exception> [__DynamicallyInvokable] public static BigInteger Parse(string value, NumberStyles style) { return BigNumber.ParseBigInteger(value, style, NumberFormatInfo.CurrentInfo); } <exception cref="T:System.ArgumentNullException">Параметр <paramref name="value"/> имеет значение null.</exception><exception cref="T:System.FormatException">Формат параметра <paramref name="value"/> является неправильным.</exception> [__DynamicallyInvokable] public static BigInteger Parse(string value, IFormatProvider provider) { return BigNumber.ParseBigInteger(value, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } <exception cref="T:System.ArgumentException">Значение параметра <paramref name="style"/> не равно значению <see cref="T:System.Globalization.NumberStyles"/>. -или- <paramref name="style"/> включает флаги <see cref="F:System.Globalization.NumberStyles.AllowHexSpecifier"/> и <see cref="F:System.Globalization.NumberStyles.HexNumber"/> вместе с другим значениями.</exception><exception cref="T:System.ArgumentNullException">Параметр <paramref name="value"/> имеет значение null.</exception><exception cref="T:System.FormatException"><paramref name="value"/> не соответствует образцу ввода, заданному <paramref name="style"/>.</exception> [__DynamicallyInvokable] public static BigInteger Parse(string value, NumberStyles style, IFormatProvider provider) { return BigNumber.ParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider)); } <exception cref="T:System.ArgumentNullException">Параметр <paramref name="value"/> имеет значение null.</exception> [__DynamicallyInvokable] public static bool TryParse(string value, out BigInteger result) { return BigNumber.TryParseBigInteger(value, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } <exception cref="T:System.ArgumentException">Значение параметра <paramref name="style"/> не равно значению <see cref="T:System.Globalization.NumberStyles"/>. -или- <paramref name="style"/> включает флаги <see cref="F:System.Globalization.NumberStyles.AllowHexSpecifier"/> и <see cref="F:System.Globalization.NumberStyles.HexNumber"/> вместе с другим значениями.</exception> [__DynamicallyInvokable] public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out BigInteger result) { return BigNumber.TryParseBigInteger(value, style, NumberFormatInfo.GetInstance(provider), out result); } [__DynamicallyInvokable] public static int Compare(BigInteger left, BigInteger right) { return left.CompareTo(right); } [__DynamicallyInvokable] public static BigInteger Abs(BigInteger value) { if (!(value >= BigInteger.Zero)) return -value; else return value; } [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static BigInteger Add(BigInteger left, BigInteger right) { return left + right; } [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static BigInteger Subtract(BigInteger left, BigInteger right) { return left - right; } [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static BigInteger Multiply(BigInteger left, BigInteger right) { return left * right; } <exception cref="T:System.DivideByZeroException">Значение параметра <paramref name="divisor"/> равно нулю (0).</exception> [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static BigInteger Divide(BigInteger dividend, BigInteger divisor) { return dividend / divisor; } <exception cref="T:System.DivideByZeroException">Значение параметра <paramref name="divisor"/> равно нулю (0).</exception> [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static BigInteger Remainder(BigInteger dividend, BigInteger divisor) { return dividend % divisor; } <exception cref="T:System.DivideByZeroException">Значение параметра <paramref name="divisor"/> равно нулю (0).</exception> [__DynamicallyInvokable] public static BigInteger DivRem(BigInteger dividend, BigInteger divisor, out BigInteger remainder) { int sign1 = 1; int sign2 = 1; BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(dividend, ref sign1); BigIntegerBuilder regDen = new BigIntegerBuilder(divisor, ref sign2); BigIntegerBuilder regQuo = new BigIntegerBuilder(); bigIntegerBuilder.ModDiv(ref regDen, ref regQuo); remainder = bigIntegerBuilder.GetInteger(sign1); return regQuo.GetInteger(sign1 * sign2); } [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static BigInteger Negate(BigInteger value) { return -value; } <exception cref="T:System.ArgumentOutOfRangeException">Натуральный логарифм <paramref name="value"/> находится вне диапазона типа данных <see cref="T:System.Double"/>.</exception> [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static double Log(BigInteger value) { return BigInteger.Log(value, Math.E); } <exception cref="T:System.ArgumentOutOfRangeException">Логарифм <paramref name="value"/> находится вне диапазона типа данных <see cref="T:System.Double"/>.</exception> [__DynamicallyInvokable] public static double Log(BigInteger value, double baseValue) { if (value._sign < 0 || baseValue == 1.0) return double.NaN; if (baseValue == double.PositiveInfinity) { return !value.IsOne ? double.NaN : 0.0; } else { if (baseValue == 0.0 && !value.IsOne) return double.NaN; if (value._bits == null) return Math.Log((double) value._sign, baseValue); double d = 0.0; double num1 = 0.5; int num2 = BigInteger.Length(value._bits); int num3 = BigInteger.BitLengthOfUInt(value._bits[num2 - 1]); int num4 = (num2 - 1) * 32 + num3; uint num5 = (uint) (1 << num3 - 1); for (int index = num2 - 1; index >= 0; --index) { while ((int) num5 != 0) { if (((int) value._bits[index] & (int) num5) != 0) d += num1; num1 *= 0.5; num5 >>= 1; } num5 = (uint) int.MinValue; } return (Math.Log(d) + 0.693147180559945 * (double) num4) / Math.Log(baseValue); } } <exception cref="T:System.ArgumentOutOfRangeException">Десятичный логарифм <paramref name="value"/> находится вне диапазона типа данных <see cref="T:System.Double"/>.</exception> [__DynamicallyInvokable] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public static double Log10(BigInteger value) { return BigInteger.Log(value, 10.0); } [__DynamicallyInvokable] public static BigInteger GreatestCommonDivisor(BigInteger left, BigInteger right) { if (left._sign == 0) return BigInteger.Abs(right); if (right._sign == 0) return BigInteger.Abs(left); BigIntegerBuilder reg1 = new BigIntegerBuilder(left); BigIntegerBuilder reg2 = new BigIntegerBuilder(right); BigIntegerBuilder.GCD(ref reg1, ref reg2); return reg1.GetInteger(1); } [__DynamicallyInvokable] public static BigInteger Max(BigInteger left, BigInteger right) { if (left.CompareTo(right) < 0) return right; else return left; } [__DynamicallyInvokable] public static BigInteger Min(BigInteger left, BigInteger right) { if (left.CompareTo(right) <= 0) return left; else return right; } <exception cref="T:System.DivideByZeroException">Значение параметра <paramref name="modulus"/> равно нулю.</exception><exception cref="T:System.ArgumentOutOfRangeException">Значение параметра <paramref name="exponent"/> является отрицательным.</exception> [__DynamicallyInvokable] public static BigInteger ModPow(BigInteger value, BigInteger exponent, BigInteger modulus) { if (exponent.Sign < 0) throw new ArgumentOutOfRangeException("exponent", SR.GetString("ArgumentOutOfRange_MustBeNonNeg")); int sign1 = 1; int sign2 = 1; int sign3 = 1; bool isEven = exponent.IsEven; BigIntegerBuilder regRes = new BigIntegerBuilder(BigInteger.One, ref sign1); BigIntegerBuilder regVal = new BigIntegerBuilder(value, ref sign2); BigIntegerBuilder bigIntegerBuilder = new BigIntegerBuilder(modulus, ref sign3); BigInt
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д