Friday, February 27, 2009

Integer square root of a positive integer in haskell

squareRoot :: Int -> Int
squareRoot n 
| n ==  1              = 1 
| otherwise            = div (k + ( div n k)) 2
where k = squareRoot(n-1) 

1 comment:

  1. I think you have an error in the otherwise
    Try this:
    | otherwise = div (k + (div (n-1) k)) 2

    ReplyDelete