The following is a bit faster than the Newton's Method solution I
suggest above. It uses a binary square root algorithm as seen here:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
The value is a perfect square if x ends up being zero.
bool isSqr(unsigned int x)
{
unsigned int res = 0;
unsigned int bit = 1 << 30;
while(bit > x) bit >>= 2;
while(bit)
{
if (x >= res + bit)
{
x -= res + bit;
res = (res >> 1) + bit;
}
else
{
res >>= 1;
}
bit >>= 2;
}
return (x == 0);
}
On Dec 23 2012, 10:37 am, Anil Sharma <[email protected]>
wrote:
> please suggest some efficient solution to check perfect square condition .
> no matter how much large number is... eg..i/p-8949 o/p-93
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.