Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #361 Test for perfect squarity

Set the boolean b to true if the integer n is a square number, false otherwise

E.g. 2025true

import math
b = math.sqrt(n).is_integer()
import static java.lang.Math.sqrt;
boolean b = sqrt(n) % 1 == 0;
math
b := round(sqrt(n)) ** 2 = n;
b := number mod (round(sqrt(number)) = 0;
class Integer
  def perfect_square?
    Integer.sqrt(self)**2 == self
  end
end

b = n.perfect_square?

New implementation...
< >
steenslag