How can the square root of a u128 be calculated? The resulting number can be a u128 after some rounding.
f64 has a f64::sqrt function, but I dont think we should be converting u128 to f64.
How can the square root of a u128 be calculated? The resulting number can be a u128 after some rounding.
f64 has a f64::sqrt function, but I dont think we should be converting u128 to f64.
You can use the Roots trait from the num crate (or directly from the num-integer crate):
pub fn sqrt(&self) -> SelfReturns the truncated principal square root of an integer –
⌊√x⌋This is solving for
rinr² = x, rounding toward zero. The result will satisfyr² ≤ x < (r+1)².
use num::integer::Roots; // 0.4.0
fn main() {
let a: u128 = 42;
let b = a.sqrt();
assert!(b == 6);
}