Shortest way to check perfect Square?

Javed Akram picture Javed Akram · Feb 3, 2011 · Viewed 30.9k times · Source

Possible Duplicate:
What's a good algorithm to determine if an input is a perfect square?

I want Shortest and Simplest way to Check a number is perfect square in C#

Some of Perfect Squares:

1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ......

Answer

Øyvind Bråthen picture Øyvind Bråthen · Feb 3, 2011

Probably checking if the square root of the number has any decimal part, or if it is a whole number.

Implementationwise, I would consider something like this:

double result = Math.Sqrt(numberToCheck);
bool isSquare = result%1 == 0;

isSquare should now be true for all squares, and false for all others.