That looks fine, or you can extend the idea further by checking quadratic
residuosity modulo a number of small integers. See
http://www.gnu.org/software/gmp/manual/html_node/Perfect-Square-Algorithm.html.

Why do you need to do lots of IsSquare() tests, I'm curious?

----- Original Message ----- From: "Jeffrey Walton" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, October 20, 2005 1:06 PM
Subject: IsSquare( )


Hi All,

I was wondering if the following would be an improvement for testing if a
number is a square (from Integer.cpp). It is a quick and dirty test. I ask
because I want to perform a lot if tests using IsSquare() on quadrtic
residues.

bool Integer::IsSquare() const
{
   Integer r;
   word digit = 0;

   if( false == IsNegative( ) ) {
       Integer::Divide(digit, r, *this, 10);

       //\ Perfect squares end in the following
       if( digit != 0 && digit != 1 && digit != 4 &&
           digit != 5 && digit != 6 && digit != 9 ) { return false; }
   }

   r = SquareRoot();
   return *this == r.Squared();
}



Reply via email to