/// <summary> /// Constructor creating a new Base10BigInteger as a copy of an existing Base10BigInteger. /// </summary> /// <param name="n">The Base10BigInteger to be copied</param> public Base10BigInteger(Base10BigInteger n) { digits = new long[MaxSize]; size = n.size; sign = n.sign; for (int i = 0; i < n.size; i++) digits[i] = n.digits[i]; }
/// <summary> /// Multiplies two Base10BigIntegers. /// </summary> private static Base10BigInteger Multiply(Base10BigInteger a, Base10BigInteger b) { int i, j; long temp, trans = 0; Base10BigInteger res = new Base10BigInteger(); res.size = a.size + b.size - 1; for (i = 0; i < res.size + 1; i++) res.digits[i] = 0; for (i = 0; i < a.size; i++) if (a.digits[i] != 0) for (j = 0; j < b.size; j++) if (b.digits[j] != 0) res.digits[i + j] += a.digits[i] * b.digits[j]; for (i = 0; i < res.size; i++) { temp = res.digits[i] + trans; res.digits[i] = temp % NumberBase; trans = temp / NumberBase; } if (trans > 0) { res.digits[res.size] = trans % NumberBase; res.size++; trans /= NumberBase; } return res; }
/// <summary> /// Subtracts the Base10BigInteger b from the Base10BigInteger a, where a >= b, a, b non-negative. /// </summary> private static Base10BigInteger Subtract(Base10BigInteger a, Base10BigInteger b) { Base10BigInteger res = new Base10BigInteger(a); int i; long temp, trans = 0; bool reducible = true; for (i = 0; i < b.size; i++) { temp = res.digits[i] - b.digits[i] - trans; if (temp < 0) { trans = 1; temp += NumberBase; } else trans = 0; res.digits[i] = temp; } for (i = b.size; ((i < a.size) && (trans > 0)); i++) { temp = res.digits[i] - trans; if (temp < 0) { trans = 1; temp += NumberBase; } else trans = 0; res.digits[i] = temp; } while ((res.size - 1 > 0) && (reducible == true)) { if (res.digits[res.size - 1] == 0) res.size--; else reducible = false; } return res; }
/// <summary> /// Determines whether the specified Base10BigInteger is equal to the current Base10BigInteger. /// </summary> /// <param name="other">The Base10BigInteger to compare with the current Base10BigInteger</param> /// <returns>True if the specified Base10BigInteger is equal to the current Base10BigInteger, /// false otherwise</returns> public bool Equals(Base10BigInteger other) { if (sign != other.sign) return false; if (size != other.size) return false; for (int i = 0; i < size; i++) if (digits[i] != other.digits[i]) return false; return true; }
/// <summary> /// Adds two BigNumbers a and b, where a >= b, a, b non-negative. /// </summary> private static Base10BigInteger Add(Base10BigInteger a, Base10BigInteger b) { Base10BigInteger res = new Base10BigInteger(a); long trans = 0, temp; int i; for (i = 0; i < b.size; i++) { temp = res.digits[i] + b.digits[i] + trans; res.digits[i] = temp % NumberBase; trans = temp / NumberBase; } for (i = b.size; ((i < a.size) && (trans > 0)); i++) { temp = res.digits[i] + trans; res.digits[i] = temp % NumberBase; trans = temp / NumberBase; } if (trans > 0) { res.digits[res.size] = trans % NumberBase; res.size++; trans /= NumberBase; } return res; }
/// <summary> /// Smaller or equal test between two Base10BigIntegers. /// </summary> /// <param name="a">The 1st Base10BigInteger</param> /// <param name="b">The 2nd Base10BigInteger</param> /// <returns>True if a <= b, false otherwise</returns> public static bool SmallerOrEqual(Base10BigInteger a, Base10BigInteger b) { return !Greater(a, b); }
/// <summary> /// Subtraction operation of two Base10BigIntegers. /// </summary> /// <param name="a">The 1st Base10BigInteger</param> /// <param name="b">The 2nd Base10BigInteger</param> /// <returns>The Base10BigInteger result of the subtraction</returns> public static Base10BigInteger Subtraction(Base10BigInteger a, Base10BigInteger b) { Base10BigInteger res = null; if ((a.sign == Sign.Positive) && (b.sign == Sign.Positive)) { if (a >= b) { res = Subtract(a, b); res.sign = Sign.Positive; } else { res = Subtract(b, a); res.sign = Sign.Negative; } } if ((a.sign == Sign.Negative) && (b.sign == Sign.Negative)) { if (a <= b) { res = Subtract(-a, -b); res.sign = Sign.Negative; } else { res = Subtract(-b, -a); res.sign = Sign.Positive; } } if ((a.sign == Sign.Positive) && (b.sign == Sign.Negative)) { if (a >= (-b)) res = Add(a, -b); else res = Add(-b, a); res.sign = Sign.Positive; } if ((a.sign == Sign.Negative) && (b.sign == Sign.Positive)) { if ((-a) >= b) res = Add(-a, b); else res = Add(b, -a); res.sign = Sign.Negative; } return res; }
/// <summary> /// Base10BigInteger inverse with respect to addition. /// </summary> /// <param name="n">The Base10BigInteger whose opposite is to be computed</param> /// <returns>The Base10BigInteger inverse with respect to addition</returns> public static Base10BigInteger Opposite(Base10BigInteger n) { Base10BigInteger res = new Base10BigInteger(n); if (res != Zero) { if (res.sign == Sign.Positive) res.sign = Sign.Negative; else res.sign = Sign.Positive; } return res; }
/// <summary> /// Multiplication operation of two Base10BigIntegers. /// </summary> /// <param name="a">The 1st Base10BigInteger</param> /// <param name="b">The 2nd Base10BigInteger</param> /// <returns>The Base10BigInteger result of the multiplication</returns> public static Base10BigInteger Multiplication(Base10BigInteger a, Base10BigInteger b) { if ((a == Zero) || (b == Zero)) return Zero; Base10BigInteger res = Multiply(Abs(a), Abs(b)); if (a.sign == b.sign) res.sign = Sign.Positive; else res.sign = Sign.Negative; return res; }
/// <summary> /// Greater or equal test between two Base10BigIntegers. /// </summary> /// <param name="a">The 1st Base10BigInteger</param> /// <param name="b">The 2nd Base10BigInteger</param> /// <returns>True if a >= b, false otherwise</returns> public static bool GreaterOrEqual(Base10BigInteger a, Base10BigInteger b) { return Greater(a, b) || Equals(a, b); }
/// <summary> /// Greater test between two Base10BigIntegers. /// </summary> /// <param name="a">The 1st Base10BigInteger</param> /// <param name="b">The 2nd Base10BigInteger</param> /// <returns>True if a > b, false otherwise</returns> public static bool Greater(Base10BigInteger a, Base10BigInteger b) { if (a.sign != b.sign) { if ((a.sign == Sign.Negative) && (b.sign == Sign.Positive)) return false; if ((a.sign == Sign.Positive) && (b.sign == Sign.Negative)) return true; } else { if (a.sign == Sign.Positive) { if (a.size > b.size) return true; if (a.size < b.size) return false; for (int i = (a.size) - 1; i >= 0; i--) if (a.digits[i] > b.digits[i]) return true; else if (a.digits[i] < b.digits[i]) return false; } else { if (a.size < b.size) return true; if (a.size > b.size) return false; for (int i = (a.size) - 1; i >= 0; i--) if (a.digits[i] < b.digits[i]) return true; else if (a.digits[i] > b.digits[i]) return false; } } return false; }
/// <summary> /// Computes the absolute value of a Base10BigInteger. /// </summary> /// <param name="n">The Base10BigInteger whose absolute value is to be computed</param> /// <returns>The absolute value of the given BigInteger</returns> public static Base10BigInteger Abs(Base10BigInteger n) { Base10BigInteger res = new Base10BigInteger(n); res.sign = Sign.Positive; return res; }