本文整理汇总了C#中BigInteger.DivideAndRemainder方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.DivideAndRemainder方法的具体用法?C# BigInteger.DivideAndRemainder怎么用?C# BigInteger.DivideAndRemainder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigInteger
的用法示例。
在下文中一共展示了BigInteger.DivideAndRemainder方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Calculate
/// <summary>
/// Runs the EEA on two BigIntegers
/// </summary>
/// <param name="A">Quotient A</param>
/// <param name="B">Quotient B</param>
/// <returns>Return a BigIntEuclidean object that contains the result in the variables X, Y, and GCD</returns>
///
/// <remarks>
/// Implemented from pseudocode on <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm"/>Wikipedia
/// </remarks>
public static BigIntEuclidean Calculate(BigInteger A, BigInteger B)
{
BigInteger x = BigInteger.Zero;
BigInteger lastX = BigInteger.One;
BigInteger y = BigInteger.One;
BigInteger lastY = BigInteger.Zero;
while (!B.Equals(BigInteger.Zero))
{
BigInteger[] quotientAndRemainder = A.DivideAndRemainder(B);
BigInteger quotient = quotientAndRemainder[0];
BigInteger temp = A;
A = B;
B = quotientAndRemainder[1];
temp = x;
x = lastX.Subtract(quotient.Multiply(x));
lastX = temp;
temp = y;
y = lastY.Subtract(quotient.Multiply(y));
lastY = temp;
}
BigIntEuclidean result = new BigIntEuclidean();
result.X = lastX;
result.Y = lastY;
result.GCD = A;
return result;
}
示例2: TestDivideAndRemainder
public void TestDivideAndRemainder()
{
// TODO More basic tests
var n = new BigInteger(48, Rnd);
BigInteger[] qr = n.DivideAndRemainder(One);
Assert.AreEqual(n, qr[0]);
Assert.AreEqual(Zero, qr[1]);
for (int rep = 0; rep < 10; ++rep)
{
var a = new BigInteger(100 - rep, 0, Rnd);
var b = new BigInteger(100 + rep, 0, Rnd);
var c = new BigInteger(10 + rep, 0, Rnd);
BigInteger d = a.Multiply(b).Add(c);
BigInteger[] es = d.DivideAndRemainder(a);
Assert.AreEqual(b, es[0]);
Assert.AreEqual(c, es[1]);
}
// Special tests for power of two since uses different code path internally
for (int i = 0; i < 100; ++i)
{
int shift = Rnd.Next(64);
BigInteger a = One.ShiftLeft(shift);
var b = new BigInteger(64 + Rnd.Next(64), Rnd);
BigInteger bShift = b.ShiftRight(shift);
BigInteger bMod = b.And(a.Subtract(One));
string data = "shift=" + shift + ", b=" + b.ToString(16);
qr = b.DivideAndRemainder(a);
Assert.AreEqual(bShift, qr[0], data);
Assert.AreEqual(bMod, qr[1], data);
qr = b.DivideAndRemainder(a.Negate());
Assert.AreEqual(bShift.Negate(), qr[0], data);
Assert.AreEqual(bMod, qr[1], data);
qr = b.Negate().DivideAndRemainder(a);
Assert.AreEqual(bShift.Negate(), qr[0], data);
Assert.AreEqual(bMod.Negate(), qr[1], data);
qr = b.Negate().DivideAndRemainder(a.Negate());
Assert.AreEqual(bShift, qr[0], data);
Assert.AreEqual(bMod.Negate(), qr[1], data);
}
}
示例3: TestDiv
private void TestDiv(BigInteger i1, BigInteger i2)
{
BigInteger q = i1.Divide(i2);
BigInteger r = i1.Remainder(i2);
BigInteger remainder;
BigInteger quotient = i1.DivideAndRemainder(i2, out remainder);
Assert.IsTrue(q.Equals(quotient), "Divide and DivideAndRemainder do not agree");
Assert.IsTrue(r.Equals(remainder), "Remainder and DivideAndRemainder do not agree");
Assert.IsTrue(q.Sign != 0 || q.Equals(zero), "signum and equals(zero) do not agree on quotient");
Assert.IsTrue(r.Sign != 0 || r.Equals(zero), "signum and equals(zero) do not agree on remainder");
Assert.IsTrue(q.Sign == 0 || q.Sign == i1.Sign * i2.Sign, "wrong sign on quotient");
Assert.IsTrue(r.Sign == 0 || r.Sign == i1.Sign, "wrong sign on remainder");
Assert.IsTrue(r.Abs().CompareTo(i2.Abs()) < 0, "remainder out of range");
Assert.IsTrue(q.Abs().Add(one).Multiply(i2.Abs()).CompareTo(i1.Abs()) > 0, "quotient too small");
Assert.IsTrue(q.Abs().Multiply(i2.Abs()).CompareTo(i1.Abs()) <= 0, "quotient too large");
BigInteger p = q.Multiply(i2);
BigInteger a = p.Add(r);
Assert.IsTrue(a.Equals(i1), "(a/b)*b+(a%b) != a");
try {
BigInteger mod = i1.Mod(i2);
Assert.IsTrue(mod.Sign >= 0, "mod is negative");
Assert.IsTrue(mod.Abs().CompareTo(i2.Abs()) < 0, "mod out of range");
Assert.IsTrue(r.Sign < 0 || r.Equals(mod), "positive remainder == mod");
Assert.IsTrue(r.Sign >= 0 || r.Equals(mod.Subtract(i2)), "negative remainder == mod - divisor");
} catch (ArithmeticException e) {
Assert.IsTrue(i2.Sign <= 0, "mod fails on negative divisor only");
}
}