public BigNumberDivisionResult Division(BigNumber num) { BigNumber abs = num.Absolute(); List <byte> t = new List <byte>(); List <byte> result = new List <byte>(); BigNumber remainder = null; for (int i = this.Length() - 1; i >= 0; i--) { t.Insert(0, this.get(i)); BigNumber a = new BigNumber(true, t); if (a.Compare(abs, true) == -1) { result.Add(0); continue; } BigNumber b = new BigNumber(); byte counter = 0; while (a.Compare(b) > 0) { counter++; b = b.Addition(abs); } if (b.Compare(a, true) > 0) { counter--; b = b.Subtraction(abs); } remainder = a.Subtraction(b); t.Clear(); for (int j = 0; j < remainder.Length(); j++) { t.Add(remainder.get(j)); } result.Add(counter); } bool orientation = true; if (this.orientation != num.orientation) { orientation = false; } return(new BigNumberDivisionResult( new BigNumber(orientation, result, true), new BigNumber(remainder) )); }
public BigNumber Modulo(BigNumber num) { BigNumberDivisionResult result = this.Division(num); return(result.Reminder()); }
public BigNumber Multiplication(BigNumber num) { int alen = this.Length(); int blen = num.Length(); List <List <byte> > rows = new List <List <byte> >(); List <byte> row = new List <byte>(); int t = 0; int space = 0; int maxlen = 0; for (int i = 0; i < blen; i++) { int c2 = num.get(i); row = new List <byte>(); t = 0; for (int k = 0; k < space; k++) { row.Add(0); } for (int j = 0; j < alen; j++) { int c1 = this.get(j); int n = c2 * c1 + t; row.Add((byte)(n % 10)); t = n / 10; } if (t > 0) { while (t > 0) { row.Add((byte)(t % 10)); t = t / 10; } } space++; if (maxlen < row.Count) { maxlen = row.Count; } rows.Add(row); } List <byte> output = new List <byte>(); t = 0; int sum = 0; for (int j = 0; j < maxlen; j++) { for (int i = 0; i < rows.Count; i++) { int value = rows[i].Count > j ? rows[i][j] : 0; sum = sum + value; } output.Add((byte)(sum % 10)); sum = sum / 10; } bool orientation = true; if (this.orientation != num.orientation) { orientation = false; } return(new BigNumber(orientation, output)); }
public BigNumber Subtraction(BigNumber num) { bool resultOrientation = true; bool switchOrder = false; bool subtraction = true; if (this.Compare(num, true) >= 0) { if (this.IsPositive() && num.IsPositive()) { subtraction = true; resultOrientation = POSITIVE; } if (this.IsPositive() && num.IsNegative()) { subtraction = false; resultOrientation = POSITIVE; } if (this.IsNegative() && num.IsPositive()) { subtraction = false; resultOrientation = NEGATIVE; } if (this.IsNegative() && num.IsNegative()) { subtraction = true; resultOrientation = NEGATIVE; } } else { switchOrder = true; if (this.IsPositive() && num.IsPositive()) { subtraction = true; resultOrientation = NEGATIVE; } if (this.IsPositive() && num.IsNegative()) { subtraction = false; resultOrientation = POSITIVE; } if (this.IsNegative() && num.IsPositive()) { subtraction = false; resultOrientation = NEGATIVE; } if (this.IsNegative() && num.IsNegative()) { subtraction = true; resultOrientation = POSITIVE; } } int alen = this.Length(); int blen = num.Length(); int len = alen > blen ? alen : blen; int t = 0; List <byte> output = new List <byte>(); for (int i = 0; i < len; i++) { int c1 = this.get(i); int c2 = num.get(i); int n = 0; if (subtraction) { if (switchOrder) { n = c2 - (c1 + t); } else { n = c1 - (c2 + t); } t = 0; if (n < 0) { n = 10 + n; t = 1; } output.Add((byte)(n)); } else { n = c1 + c2 + t; t = 0; if (n > 9) { t = n / 10; } output.Add((byte)(n % 10)); } } return(new BigNumber(resultOrientation, output)); }
public BigNumberDivisionResult(BigNumber division, BigNumber reminder) { this.division = new BigNumber(division); this.reminder = new BigNumber(reminder); }
public bool IsSmallerOrEqual(BigNumber num, bool skipOrientation = false) { return(this.Compare(num, skipOrientation) <= 0); }
public bool IsSmaller(BigNumber num, bool skipOrientation = false) { return(this.Compare(num, skipOrientation) == -1); }