# Real O(1) just for fun

• ``````class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
r = num
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
r = (r + num / r) / 2
return r * r == num
``````

• This is a smart solution! Good job, man.

• Interesting.......

• Can someone explain how this works?!

• @takirala It's Newton Approximation. Twentish iterations can ensure returning the correct answer. This is just for fun.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.