Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Solved

Posted on 2003-11-21

How to find if a number is a perfect square?

I am NOT looking for the solution which says check if sqrt is integer or not

I am NOT looking for the solution which says check if sqrt is integer or not

9 Comments

I don't want this solution

int n;

cin >> n;

if (sqrt (N) == Whole Number)

cout << "A perfect square"

else

cout << "Not a perfect square"

I was hoping more in terms of bit wise operators (>>,<<,&,|) etc

I actually read the solution in terms of bit wise operators many weeks ago. I could not recall the solution.

This is not due as in for assignment or hw. Hence I alotted only few points. Take your time and hopefully provide a solution i am looking for.

K

http://www.ticalc.org/pub/83plus/asm/source/routines/_fastsqrt_.zip

I came across this website which might be closer to what you are looking for.

http://www.fifi.org/cgi-bin/info2www?(gmp)Perfect+Square+Algorithm

It is possible that you remember seeing the bitwise solution for checking whether the number is power of 2 - it would be indeed simple in bitwise terms:

M is 2^N if and only if (M & (M-1)) equals zero (assuming N>0).

For N^2 I cannot remember anything simple (if I do, I'll let you know, of course).

Regards

l8rz... suma

I will leave the following recommendation for this question in the Cleanup topic area:

PAQ - no points refunded (of 50)

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Venabili

EE Cleanup Volunteer

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

Quick: What are the IT costs associated with moving to a new building? | 9 | 32 | |

Recommendation vb6 to vb.net or others | 14 | 141 | |

Regular Expression Calculator Tester | 2 | 71 | |

Looking for a network enabled locker remotely controlled like Amazon locker | 2 | 73 |

A short article about a problem I had getting the GPS LocationListener working.

If you don't know how to downgrade, my instructions below should be helpful.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**9** Experts available now in Live!