-
rust연습] pi(𝜋)파이 1억자리까지 계산 하기코딩Coding/Rust연습 2022. 4. 16. 22:43728x90
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
반응형'코딩Coding > Rust연습' 카테고리의 다른 글
Rust연습] cow_빨라서 최고borrow에서 적극 활용하자 (0) 2022.04.19 rust연습]vec벡터 끼리 합치기 + vec + vec append (0) 2022.04.18 rust연습]u8_sting_stack저장하기bytes() / std::str (0) 2022.04.18 rust연습]10진수 숫자 16진수로 알아보는 방법& 16진수를 10진수 숫자로 알아보는 방법 (0) 2022.04.17 Rust연습] 섀도 변수 y를 생성하는 match 표현식 (0) 2022.04.11 Rust연습] Some(String) Some(_) 연습 (0) 2022.04.11 rust 연습] iter(), zip() 연습 (0) 2022.04.11 Rust연습] 16진수출력❤️HexRGB(Color) 연습출처 : Rust 101 - Lecture 1 (0) 2022.04.04