@Gaurav: Does it work for n = 25 or for n = 8? I think you've confused "perfect square" with "power of two." Dave
On Wednesday, February 6, 2013 11:32:55 PM UTC-6, Gaurav Rana wrote: > if(n&(n-1)==0) > //perfect square > > > On Thu, Feb 7, 2013 at 3:01 AM, Don <[email protected] <javascript:>>wrote: > >> 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] <javascript:>. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > -- 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.
