From df97d975bc9e19fdf87ac630762f6d3709125e59 Mon Sep 17 00:00:00 2001 From: Hammy Date: Wed, 12 Jan 2022 01:29:32 +0000 Subject: [PATCH] Add Fibonacci --- fibonacci/Cargo.toml | 8 ++++++++ fibonacci/src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 fibonacci/Cargo.toml create mode 100644 fibonacci/src/main.rs diff --git a/fibonacci/Cargo.toml b/fibonacci/Cargo.toml new file mode 100644 index 0000000..9cd048e --- /dev/null +++ b/fibonacci/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fibonacci" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/fibonacci/src/main.rs b/fibonacci/src/main.rs new file mode 100644 index 0000000..73764d0 --- /dev/null +++ b/fibonacci/src/main.rs @@ -0,0 +1,39 @@ +use std::io; + +fn main() { + fibonacci(); +} + +fn fibonacci() { + let (start, end): (u32, u32) = get_fibonacci_range(); + print!("{}..{} Fibonacci Numbers: [", start, end); + + for index in start..=end { + if index == end { + print!("{}", get_next_num(index)); + } else { + print!("{}, ", get_next_num(index)); + } + } + + print!("]"); +} + +fn get_next_num(n: u32) -> u32 { + if n == 0 || n == 1 { + return n; + } + return get_next_num(n - 1) + get_next_num(n - 2); +} + +fn get_fibonacci_range() -> (u32, u32) { + let mut buffer: String = String::new(); + + println!("Enter Range of Fibonacci Numbers To View\nFormat -> [u32 u32] E.g 1 5"); + io::stdin() + .read_line(&mut buffer) + .expect("Failed To Read In Line"); + + let ranges: Vec<&str> = buffer.split(" ").collect(); + return (ranges[0].trim().parse().unwrap(), ranges[1].trim().parse().unwrap()); +} \ No newline at end of file