코딩Coding/Rust연습
rust연습] pi(𝜋)파이 1억자리까지 계산 하기
내인생PLUS
2022. 4. 16. 22:43
728x90
code 시작
1 use std::time::Instant;
2
3 const N: u64 = 100_000_000;
4
5 fn calculate_pi(n_terms: u64) -> f64 {
6 let numerator = 4.0;
7 let mut denominator = 1.0;
8 let mut operation = 1.0;
9 let mut pi = 0.0;
10 for _ in 0..n_terms {
11 pi += operation * (numerator / denominator);
12 denominator += 2.0;
13 operation *= -1.0;
14 }
15 pi
16 }
17
18 fn main() {
19 let start = Instant::now();
20 let pi = calculate_pi(N);
21 let duration = start.elapsed();
22 println!("{pi}");
23 println!("Time elapsed : {:.2} seconds! ", duration.as_secs_f64());
24 }
use std::time::Instant;
const N: u64 = 100_000_000;
fn calculate_pi(n_terms: u64) -> f64 {
let numerator = 4.0;
let mut denominator = 1.0;
let mut operation = 1.0;
let mut pi = 0.0;
for _ in 0..n_terms {
pi += operation * (numerator / denominator);
denominator += 2.0;
operation *= -1.0;
}
pi
}
fn main() {
let start = Instant::now();
let pi = calculate_pi(N);
let duration = start.elapsed();
println!("{pi}");
println!("Time elapsed : {:.2} seconds! ", duration.as_secs_f64());
}
Result:
LiveCoding
러스트Rust한글강의_제로코스트추상화중요성_이해zero cost abstractions#rust #abstractions
반응형