public void testMaxMin() { MyInt a = new MyInt(200); MyInt b = new MyInt(199); MyInt c = new MyInt("201"); MyInt d = new MyInt(-200); Assert.AreEqual(b.max(a), a.max(b)); Assert.AreEqual(a.max(b), a); Assert.AreEqual(c.max(a), c); Assert.AreEqual(c.max(b), c); Assert.AreEqual(b.max(d), b); Assert.AreEqual(c.max(c), c); Assert.AreEqual(b.min(d), d.min(b)); Assert.AreEqual(a.min(b), b); Assert.AreEqual(a.min(c), a); Assert.AreEqual(d.min(a), d); Assert.AreEqual(c.min(d), d); Assert.AreEqual(c.min(c), c); }
public MyInt gcd(MyInt b) { MyInt a = abs(); b = b.abs(); if (a.compareTo(new MyInt(0)) || b.compareTo(new MyInt(0))) { return(new MyInt(1)); } while (!a.compareTo(b)) { if (a.max(b) == a) { a = a.substract(b); } else { b = b.substract(a); } } return(a); }
public MyInt divide(MyInt b) { if (b.compareTo(new MyInt(0))) { return(new MyInt(0)); } byte[] valueA = getValue(); byte[] valueB = b.getValue(); List <byte> result = new List <byte>(); bool isPositiveA = valueA[0] == 0; bool isPositiveB = valueB[0] == 0; valueA = valueA.Skip(1).ToArray(); valueB = valueB.Skip(1).ToArray(); int start = 0; int end = 1; while (end <= valueA.Length) { List <byte> tempAValue = valueA.Skip(start).Take(end - start).ToList();; tempAValue.Insert(0, 0); List <byte> tempBValue = valueB.ToList(); tempBValue.Insert(0, 0); MyInt tempA = new MyInt(tempAValue.ToArray()); MyInt tempB = new MyInt(tempBValue.ToArray()); if (tempA.max(tempB).compareTo(tempA)) { MyInt resultDivide = new MyInt(1); while (!tempB.multiply(resultDivide).min(tempA).compareTo(tempA)) { resultDivide = resultDivide.add(new MyInt(1)); } if (!resultDivide.compareTo(new MyInt(1))) { resultDivide = resultDivide.substract(new MyInt(1)); } result.AddRange(resultDivide.getValue().Skip(1)); MyInt resultMultiply = resultDivide.multiply(tempB); MyInt resultDivideRest = tempA.substract(resultMultiply); start = end - resultDivideRest.getValue().Length + 1; for (int i = 0; i + start < end; i++) { valueA[i + start] = resultDivideRest.getValue()[i + 1]; } } end++; } if (result.Count == 0) { result.Insert(0, 0); } result.Insert(0, (byte)(isPositiveA == isPositiveB ? 0 : 1)); return(new MyInt(result.ToArray())); }