Created
February 2, 2019 06:20
-
-
Save reverland/900ea76af9f26aab50c764c2b1dddc90 to your computer and use it in GitHub Desktop.
get pi with fib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f64; | |
fn rnd10(f: f64) -> f64 { (f * 1e10).round() / 1e10 } | |
fn iter_pi(epsilon: f64) -> (i32, f64) { | |
let mut x = 0.0; | |
let mut fib = vec![0, 1]; | |
let mut cnt = 0; | |
loop { | |
if cnt >= 1 { | |
let n = fib[2 * cnt - 2] + fib[2 * cnt - 1]; | |
fib.push(n); | |
let n = fib[2 * cnt] + fib[2 * cnt - 1]; | |
fib.push(n); | |
} | |
x += 4. * (1.0 / fib[2 * cnt + 1] as f64).atan(); | |
println!("{}", x); | |
if (x - f64::consts::PI).abs() < epsilon { | |
return ((cnt + 1) as i32, x); | |
} | |
cnt += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment