Skip to content

Commit 3833082

Browse files
committed
add 69
1 parent 589e84a commit 3833082

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

S0069-sqrtx/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
4+
5+
struct Solution;
6+
//https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division
7+
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

Comments
 (0)