예제 #1
0
        private mpz_t Sqrt(mpz_t n)
        {
            if (n == 0)
            {
                return(0);
            }

            if (n > 0)
            {
                int   bitLength = Convert.ToInt32(Math.Ceiling(BigInteger.Log(n.ToBigInteger(), 2)));
                mpz_t root      = mpz_t.One << (bitLength / 2);

                while (!IsSqrt(n, root))
                {
                    root += n / root;
                    root /= 2;
                }

                return(root);
            }

            throw new ArithmeticException("NaN");
        }