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


C# BigInteger.sqrt方法代码示例

本文整理汇总了C#中Granados.BigInteger.sqrt方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.sqrt方法的具体用法?C# BigInteger.sqrt怎么用?C# BigInteger.sqrt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Granados.BigInteger的用法示例。


在下文中一共展示了BigInteger.sqrt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SqrtTest

        //***********************************************************************
        // Tests the correct implementation of sqrt() method.
        //***********************************************************************

        public static void SqrtTest(int rounds) {
            Random rand = new Random();
            for (int count = 0; count < rounds; count++) {
                // generate data of random length
                int t1 = 0;
                while (t1 == 0)
                    t1 = (int)(rand.NextDouble() * 1024);

                Console.Write("Round = " + count);

                BigInteger a = new BigInteger();
                a.genRandomBits(t1, rand);

                BigInteger b = a.sqrt();
                BigInteger c = (b + 1) * (b + 1);

                // check that b is the largest integer such that b*b <= a
                if (c <= a) {
                    Console.WriteLine("\nError at round " + count);
                    Console.WriteLine(a + "\n");
                    return;
                }
                Console.WriteLine(" <PASSED>.");
            }
        }
开发者ID:Ricordanza,项目名称:poderosa,代码行数:29,代码来源:BigInteger.cs

示例2: LucasStrongTestHelper

        private bool LucasStrongTestHelper(BigInteger thisVal) {
            // Do the test (selects D based on Selfridge)
            // Let D be the first element of the sequence
            // 5, -7, 9, -11, 13, ... for which J(D,n) = -1
            // Let P = 1, Q = (1-D) / 4

            long D = 5, sign = -1, dCount = 0;
            bool done = false;

            while (!done) {
                int Jresult = BigInteger.Jacobi(D, thisVal);

                if (Jresult == -1)
                    done = true;    // J(D, this) = 1
                else {
                    if (Jresult == 0 && Math.Abs(D) < thisVal)       // divisor found
                        return false;

                    if (dCount == 20) {
                        // check for square
                        BigInteger root = thisVal.sqrt();
                        if (root * root == thisVal)
                            return false;
                    }

                    //Console.WriteLine(D);
                    D = (Math.Abs(D) + 2) * sign;
                    sign = -sign;
                }
                dCount++;
            }

            long Q = (1 - D) >> 2;

            /*
                Console.WriteLine("D = " + D);
                Console.WriteLine("Q = " + Q);
                Console.WriteLine("(n,D) = " + thisVal.gcd(D));
                Console.WriteLine("(n,Q) = " + thisVal.gcd(Q));
                Console.WriteLine("J(D|n) = " + BigInteger.Jacobi(D, thisVal));
                */

            BigInteger p_add1 = thisVal + 1;
            int s = 0;

            for (int index = 0; index < p_add1.dataLength; index++) {
                uint mask = 0x01;

                for (int i = 0; i < 32; i++) {
                    if ((p_add1.data[index] & mask) != 0) {
                        index = p_add1.dataLength;      // to break the outer loop
                        break;
                    }
                    mask <<= 1;
                    s++;
                }
            }

            BigInteger t = p_add1 >> s;

            // calculate constant = b^(2k) / m
            // for Barrett Reduction
            BigInteger constant = new BigInteger();

            int nLen = thisVal.dataLength << 1;
            constant.data[nLen] = 0x00000001;
            constant.dataLength = nLen + 1;

            constant = constant / thisVal;

            BigInteger[] lucas = LucasSequenceHelper(1, Q, t, thisVal, constant, 0);
            bool isPrime = false;

            if ((lucas[0].dataLength == 1 && lucas[0].data[0] == 0) ||
                (lucas[1].dataLength == 1 && lucas[1].data[0] == 0)) {
                // u(t) = 0 or V(t) = 0
                isPrime = true;
            }

            for (int i = 1; i < s; i++) {
                if (!isPrime) {
                    // doubling of index
                    lucas[1] = thisVal.BarrettReduction(lucas[1] * lucas[1], thisVal, constant);
                    lucas[1] = (lucas[1] - (lucas[2] << 1)) % thisVal;

                    //lucas[1] = ((lucas[1] * lucas[1]) - (lucas[2] << 1)) % thisVal;

                    if ((lucas[1].dataLength == 1 && lucas[1].data[0] == 0))
                        isPrime = true;
                }

                lucas[2] = thisVal.BarrettReduction(lucas[2] * lucas[2], thisVal, constant);     //Q^k
            }


            if (isPrime) {     // additional checks for composite numbers
                // If n is prime and gcd(n, Q) == 1, then
                // Q^((n+1)/2) = Q * Q^((n-1)/2) is congruent to (Q * J(Q, n)) mod n

                BigInteger g = thisVal.gcd(Q);
//.........这里部分代码省略.........
开发者ID:Ricordanza,项目名称:poderosa,代码行数:101,代码来源:BigInteger.cs


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