sqrt() public method

public sqrt ( ) : BigInteger
return BigInteger
Ejemplo n.º 1
0
        public void TestSqrt()
        {
            BigInteger bi;
            int        val, sqrtVal;
            Random     rand = new Random();

            for (int i = 0; i < 100; i++)
            {
                val     = rand.Next();
                bi      = new BigInteger(val);
                sqrtVal = (int)Math.Floor(Math.Sqrt(val));
                Assert.AreEqual(sqrtVal, bi.sqrt());
            }

            bi = new BigInteger();
            Assert.AreEqual(0, bi.sqrt());

            bi = new BigInteger("48234798239584935745984795837", 10);
            Assert.AreEqual(219624220521291, bi.sqrt());

            bi = new BigInteger("4823479823958493574598479580945895480904590958034958034580948509485094850934095809458408509485094850948509803459834037", 10);
            Assert.AreEqual("69451276618637425696010359184467375646677653070095660334837", bi.sqrt().ToString());

            bi = new BigInteger("902380594730957598498379487239749823749832749823749823759823759823649623984623974627682368236423764823649823749823749823794872398472398479238479382749823794823794823749823794823794872398479238479823749823749823749823749823749823740239480293840923804923804923809482304982", 10);
            Assert.AreEqual("949937153042746085485800690340716910200218535446376464883006159759187016711766033117259286191698487700345112712284215083646265481183724", bi.sqrt().ToString());
        }
Ejemplo n.º 2
0
        public override IObject PowerOperator(IObject rightSide)
        {
            switch (rightSide.IType)
            {
            case IObjectType.I_Float:
                double value = ((I_Float)rightSide).VALUE;
                if (value == 0.5f)
                {
                    return(new I_Int(bigValue.sqrt()));
                }
                return(new I_Float(Math.Pow(VALUE, value)));

            case IObjectType.I_Int:
                try
                {
                    BigInteger tmp = bigValue.Power((uint)((I_Int)rightSide));
                    return(new I_Int(tmp));
                }
                catch
                {
                    return(new I_Error("Overflow"));
                }

            default:
                return(new I_Error("Calculation error"));
            }
        }
Ejemplo n.º 3
0
        public List <BigInteger> GeneraP()
        {
            BigInteger        _inizio = 1000000;
            BigInteger        _fine   = 1010000;
            BigInteger        _limite = 0;
            List <BigInteger> _primi  = new List <BigInteger>();
            BigInteger        _primo  = 0;

            for (BigInteger i = _inizio; i <= _fine; i++)
            {
                bool primo = true;
                if (!pari(i))
                {
                    _limite = (i.sqrt()) + 1;
                    for (int j = 3; (j <= _limite) && (primo); j++)
                    {
                        if (i % j == 0)
                        {
                            primo = false;
                        }
                    }
                }
                else
                {
                    primo = false;
                }
                if (primo)
                {
                    _primi.Add(i);
                }
            }
            return(_primi);
        }
Ejemplo n.º 4
0
 protected void handler_buttonSqrtXClick(object sender, EventArgs e)
 {
     try{
         parseValues(true, true);
         labelResult.Text = ((x.sqrt()) % n).ToString();
     } catch {
         clearFields();
         this.Navigation.PushAsync(new Error(this, errorText));
     }
 }
Ejemplo n.º 5
0
 //1
 public static bool IsPrime(this BigInteger number)
 {
     if (number < 2)
     {
         return(false);
     }
     for (BigInteger i = 2; i < number.sqrt(); i++)
     {
         if (number % i == 0)
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 6
0
        //***********************************************************************
        // 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>.");
	        }
        }
Ejemplo n.º 7
0
        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);
                        if(g.dataLength == 1 && g.data[0] == 1)         // gcd(this, Q) == 1
                        {
                                if((lucas[2].data[maxLength-1] & 0x80000000) != 0)
                                        lucas[2] += thisVal;

                                BigInteger temp = (Q * BigInteger.Jacobi(Q, thisVal)) % thisVal;
                                if((temp.data[maxLength-1] & 0x80000000) != 0)
                                        temp += thisVal;

                                if(lucas[2] != temp)
                                        isPrime = false;
                        }
                }

                return isPrime;
        }