public CustomBigUInt Divide(CustomBigUInt number) { if (number == 0) { throw new DivideByZeroException(); } CustomBigUInt left = new CustomBigUInt(0), right = new CustomBigUInt(this); while (left < right) { var middle = (left + right + 1).Divide(2); if (middle * number > this) { right = middle - 1; } else { left = middle; } } left.Trim(); return(left); }
public CustomBigUInt Multiply(CustomBigUInt number) { var result = new CustomBigUInt(0); int carry = 0; for (int i = 0; i < this.Digits.Length; i++) { for (int j = 0; j < number.Digits.Length || carry > 0; j++) { if (result.Digits.Length <= i + j) { result.Digits.Add(0); } if (j < number.Digits.Length && i < this.Digits.Length) { result.Digits[i + j] += this.Digits[i] * number.Digits[j]; } result.Digits[i + j] += carry; carry = result.Digits[i + j] / _baseNumber; result.Digits[i + j] %= _baseNumber; } } result.Trim(); return(result); }
public CustomBigUInt Add(CustomBigUInt number) { var result = new CustomBigUInt(this); int carry = 0; for (int i = 0; i < number.Digits.Length || carry != 0; i++) { if (result.Digits.Length <= i) { result.Digits.Add(0); } if (i < number.Digits.Length) { result.Digits[i] += number.Digits[i]; } result.Digits[i] += carry; carry = result.Digits[i] / _baseNumber; result.Digits[i] %= _baseNumber; } result.Trim(); return(result); }
public CustomBigUInt(CustomBigUInt number) { foreach (var digit in number.Digits) { this.Digits.Add(digit); } this.Trim(); }
static void Main(string[] args) { Console.WriteLine("Enter two numbers (one line)"); string[] input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries). ToArray(); var a = new CustomBigUInt(input[0]); var b = new CustomBigUInt(input[1]); Console.WriteLine($"LCM = {a * b / GCD(a, b)}"); Console.ReadKey(); }
static CustomBigUInt GCD(CustomBigUInt a, CustomBigUInt b) { while (a != 0 && b != 0) { if (a > b) { a %= b; } else { b %= a; } } return(a + b); }
private CustomBigUInt Divide(long number) { var result = new CustomBigUInt(this); int carry = 0; for (int i = this.Digits.Length - 1; i >= 0; i--) { long curr = result.Digits[i] + carry * _baseNumber; result.Digits[i] = (int)(curr / number); carry = (int)(curr % number); } result.Trim(); return(result); }
public CustomBigUInt Minus(CustomBigUInt number) { if (this < number) { throw new ArgumentOutOfRangeException("Число не может получиться меньше чем 0!"); } var result = new CustomBigUInt(this); int carry = 0; for (int i = 0; i < number.Digits.Length || carry > 0; i++) { if (result.Digits.Length <= i) { result.Digits.Add(0); } if (i < number.Digits.Length) { result.Digits[i] -= number.Digits[i]; } result.Digits[i] -= carry; if (result.Digits[i] < 0) { carry = 1; result.Digits[i] += _baseNumber; } else { carry = 0; } } result.Trim(); return(result); }
public override bool Equals(object obj) { CustomBigUInt number = obj as CustomBigUInt; if (number == null) { return(false); } if (number.Digits.Length != Digits.Length) { return(false); } for (int i = 0; i < Digits.Length; i++) { if (Digits[i] != number.Digits[i]) { return(false); } } return(true); }
private CustomBigUInt Mod(CustomBigUInt number) { return(this - this / number * number); }