Skip to content

Instantly share code, notes, and snippets.

@skhokhlov
Created February 6, 2015 19:08
Show Gist options
  • Save skhokhlov/920592ee50f89cc5b407 to your computer and use it in GitHub Desktop.
Save skhokhlov/920592ee50f89cc5b407 to your computer and use it in GitHub Desktop.
The calculation of the number pi
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