Created
August 8, 2018 12:27
-
-
Save 0V/d1f31a2f00891061d1f7a7c98b65387b to your computer and use it in GitHub Desktop.
Wallis' Formula by Rust
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::io; | |
fn main() { | |
println!("Please Input Count > "); | |
let mut count = String::new(); | |
io::stdin().read_line(&mut count).expect("Failed to read line");; | |
let count: u64 = count.trim().parse().expect("Need u64"); | |
println!("PI = {}",wallis_fomula(count)); | |
} | |
fn wallis_fomula(count : u64) -> f64 { | |
let mut pi : f64 = 1.0; | |
for i in 1..count { | |
let n = (4 * i * i) as f64; | |
pi *= n / (n - 1.0); | |
} | |
let ans = pi * 2.0; | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment