[back]

Integer Square Root

Implement the function

val sqrt : int -> int

so that sqrt n is the smallest nonnegative integer \(i\) such that \(i^2 \geq n\). The behavior of the function is undefined if n is negative. You may not convert n into a floating-point number.

Examples

let _ = assert (sqrt 4 = 2)
let _ = assert (sqrt 9 = 3)
let _ = assert (sqrt 100 = 10)
let _ = assert (sqrt 2 = 2)
let _ = assert (sqrt 10 = 4)
let _ = assert (sqrt 99 = 10)