-
Rust – Fibonacci using Recursion and Iteration코딩Coding/Rust연습 2022. 6. 26. 05:25728x90
Rust – Fibonacci using Recursion and Iteration
use std::time::{Duration, Instant, SystemTime}; fn fibonacci_recusive(n: i64) -> i64 { if n < 2 { return n; } return fibonacci_recusive(n - 1) + fibonacci_recusive(n - 2); } fn fibonacci_iterative(n: i64) -> i64 { let mut first_number: i64 = 0; let mut second_number: i64 = 0; let mut current_number: i64 = 1; let mut i: i64 = 1; while i < n { first_number = second_number; second_number = current_number; current_number = first_number + second_number; i = i + 1; } current_number } fn main() { let start_sys_time_recursive = Instant::now(); println!("{}", fibonacci_recusive(50)); println!( "Recursive {:?}", start_sys_time_recursive.elapsed().as_millis() ); let start_sys_time_iterative = Instant::now(); println!("{}", fibonacci_iterative(50)); println!( "Recursive {:?}", start_sys_time_iterative.elapsed().as_millis() ); }
https://turreta.com/2019/10/05/rust-fibonacci-using-recursion-and-iteration/
반응형'코딩Coding > Rust연습' 카테고리의 다른 글
Rust연습❤️HashMap, Arc, Mutex, thread연습 (0) 2022.08.13 Rust연습❤️] Hello from asm__assembly코드 연습 (0) 2022.07.03 Rust연습❤️] Displaying raw pointers (0) 2022.07.03 Rust연습❤️내 컴퓨터에 병렬 실행 가능한 코어수 알아보기available_parallelism (0) 2022.06.29 Rust연습❤️) Collect & Vec 조합 Closure활용 (0) 2022.06.23 Rust연습)그래프 그리기 + 움직이는 별 표현하기(움직이는 gif만들기) (0) 2022.06.15 Rust연습] tuple연습❤️ & ndarray ❤️연습 (0) 2022.06.02 Rust연습] Cow__120 Easy Rust in Korean: to_mut with Cow (0) 2022.04.19