def isPerfectSquare(x):
i = 0
while i * i < x:
i += 1
return i * i == x
This is such a beautiful code.
Is it??? How about this.
def isPerfectSquare(x):
xRoot = int(x ** 0.5 + 0.1)
return xRoot * xRoot == x
or in my favourite lambda way,
isPerfectSquare = lambda x: int(x ** 0.5 + 0.1) ** 2 == x
If you know what is going on, you'll love the recipe. If you don't then start thinking.