367.有效的完全平方数 修改

This commit is contained in:
游由 2021-11-11 14:50:59 +08:00
parent a01faf119e
commit 69ded32ad3
1 changed files with 10 additions and 3 deletions

View File

@ -42,12 +42,19 @@ impl Solution {
}
x0 = x1;
}.pow(2) == num*/
let h = 0.5 * num as f64;
/*let h = 0.5 * num as f64;
let mut f = f64::from_bits(0x5FE6EC85E7DE30DA - ((num as f64).to_bits() >> 1));
f = f * (1.5 - h * f * f);
f = f * (1.5 - h * f * f);
f = f * (1.5 - h * f * f);
((1.0 / f) as i32).pow(2) == num
((1.0 / f) as i32).pow(2) == num*/
let x_half = 0.5 * num as f32;
let mut i = (num as f32).to_bits();
i = 0x1FBD1DF5 + (i >> 1);
let mut f = f32::from_bits(i);
f = 0.5 * f + x_half / f;
f = 0.5 * f + x_half / f;
(f as i32).pow(2) == num
}
}
@ -58,6 +65,6 @@ mod test {
#[test]
fn test_q0367() {
assert_eq!(Solution::is_perfect_square(16), true);
assert_eq!(Solution::is_perfect_square(2147395600), true);
}
}