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, ......
You can also tell if a number is a perfect square by finding its square roots. Finding the square root is the inverse (opposite) of squaring a number. If you find the square root of a number and it's a whole integer, that tells you that the number is a perfect square. For instance, the square root of 25 is 5.
double result = Math. Sqrt(numberToCheck); bool isSquare = result%1 == 0; isSquare should now be true for all squares, and false for all others.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With