, user2989737, n . , O (n). 0 , O (sqrt n):
intSquareRoot :: Int -> Int
intSquareRoot n = try 0 where
try i | i*i <= n = try (i + 1)
| True = i - 1
( ):
squareRoot :: Integral t => t -> t
squareRoot n
| n > 0 = babylon n
| n == 0 = 0
| n < 0 = error "Negative input"
where
babylon a | a > b = babylon b
| True = a
where b = quot (a + quot n a) 2
, ( GNU), . , n.
To do this, I generalized it to any type of Integral, checked for negative input and checked n == 0 to avoid dividing by 0.
source
share