当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.DivideAndRemainder方法代码示例

本文整理汇总了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;
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:42,代码来源:BigIntEuclidean.cs

示例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);
            }
        }
开发者ID:ChemicalRocketeer,项目名称:BigMath,代码行数:49,代码来源:BigIntegerTest.cs

示例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");
            }
        }
开发者ID:deveel,项目名称:deveel-math,代码行数:29,代码来源:BigIntegerTest.cs


注:本文中的BigInteger.DivideAndRemainder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。