1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! integer square root with newton's method
pub fn isqrt(n: u64) -> u64 {
let mut x0 = n >> 1;
if x0 == 0 {
return n;
}
loop {
let x1 = (x0 + n / x0) >> 1;
if x1 >= x0 {
break;
}
x0 = x1;
}
x0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
use crate::integer_square_root_with_binary_search_u64::isqrt as binary;
for i in 0..1000 {
assert_eq!(isqrt(i), binary(i));
}
}
}