Created
February 6, 2015 19:08
-
-
Save skhokhlov/920592ee50f89cc5b407 to your computer and use it in GitHub Desktop.
The calculation of the number pi
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
fn main(){ | |
let mut pi = 0f64; | |
let mut n = 0u64; | |
let mut a = 1f64; | |
let mut b = 1f64; | |
let mut k = 0f64; | |
loop { | |
a = inv(-1f64, n); | |
b = inv(4f64, n); | |
pi = pi + a/b*(2f64/(4f64*k+1f64)+2f64/(4f64*k+2f64)+1f64/(4f64*k+3f64)); | |
n = n + 1u64; | |
if n > 100{ | |
break; | |
} | |
k = k + 1f64; | |
} | |
println!("{}", pi); | |
} | |
fn inv(a: f64, n: u64) -> f64 { | |
let mut res = 1f64; | |
let mut k = n; | |
loop { | |
if k < 1 { | |
break; | |
} | |
res = res * a; | |
k = k - 1; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment