Perfect Square Number

Perfect square number is an integer that is the square of an integer. For example we can say that 25 is a perfect square number, since it is a square of 5.
5 * 5 = 25
We can also say that the square root of a perfect square number is also a integer.
√36 = 6

We can check if a number is perfect square or not. First we get the square root of the number, then we multiply the result by itself, if it is a squre number then it shuld macth to original number.
√36 = 6
6 * 6 = 36 [36 is a perfect square number]
√35 = 5.9160 (cast it into long) = 5
5 * 5 ≠ 35 [whether 35 is not]

Java implementation of this method is,

boolean isPSN(long num) {

  if (num < 0)
    return false;

  long sqr = (long) Math.sqrt(num);
  return sqr * sqr == num;
}

S. Mahbub – Uz – Zaman
Monday, September 26, 2011

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s