We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 589e84a commit 3833082Copy full SHA for 3833082
S0069-sqrtx/Cargo.toml
@@ -0,0 +1,7 @@
1
+[package]
2
+name = "S0069-sqrtx"
3
+version = "0.1.0"
4
+authors = ["xargin <cao1988228@163.com>"]
5
+edition = "2018"
6
+
7
+[dependencies]
S0069-sqrtx/src/main.rs
@@ -0,0 +1,18 @@
+fn main() {
+ println!("Hello, world!");
+}
+struct Solution;
+//https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
+impl Solution {
8
+ pub fn my_sqrt(x: i32) -> i32 {
9
+ let x = x as f64;
10
+ let (mut a1, mut a2) = (1.0f64, 0.0f64);
11
12
+ while (a1 - a2).abs() >= 0.1 {
13
+ a2 = a1;
14
+ a1 = (a1 + x / a1) / 2.0;
15
+ }
16
+ a1 as i32
17
18
0 commit comments