From 497ca00d24d738cdaf928af671175af282f6aff1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 21 Feb 2019 14:32:17 -0500 Subject: [PATCH] Add recursive fibonnachi example --- .idea/misc.xml | 1 + .idea/rust.iml | 5 ++ .idea/workspace.xml | 133 +++++++++++++++++++++++++++--------------- fibonacci/Cargo.toml | 8 +++ fibonacci/src/main.rs | 52 +++++++++++++++++ 5 files changed, 152 insertions(+), 47 deletions(-) create mode 100644 fibonacci/Cargo.toml create mode 100644 fibonacci/src/main.rs diff --git a/.idea/misc.xml b/.idea/misc.xml index 7824de4..c8116fe 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -45,6 +45,7 @@ + diff --git a/.idea/rust.iml b/.idea/rust.iml index 952a6f9..e6bb3c9 100644 --- a/.idea/rust.iml +++ b/.idea/rust.iml @@ -181,6 +181,10 @@ + + + + @@ -194,6 +198,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c359113..fa4946c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -14,11 +14,38 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -56,12 +83,15 @@ expensive_closure(intensity) ordinals /// + u128 + u64 f_to_c expensive_result.value(intensity) ORDINALS //! + u128 @@ -70,8 +100,6 @@ @@ -131,10 +161,10 @@ false - @@ -177,12 +207,23 @@ - + - + + + + + + + + + + + + @@ -215,7 +256,7 @@ - + @@ -249,7 +290,7 @@ - + - + @@ -327,11 +368,11 @@ + - @@ -368,11 +409,11 @@ - + - - + + @@ -380,7 +421,7 @@ - + @@ -402,27 +443,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -715,9 +735,7 @@ - - - + @@ -803,6 +821,13 @@ + + + + + + + @@ -810,6 +835,13 @@ + + + + + + + @@ -817,10 +849,17 @@ - + - - + + + + + + + + + diff --git a/fibonacci/Cargo.toml b/fibonacci/Cargo.toml new file mode 100644 index 0000000..cd79706 --- /dev/null +++ b/fibonacci/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fibonacci" +version = "0.1.0" +authors = ["Timothy Warren "] +edition = "2018" + +[dependencies] +separator = "0.4.0" diff --git a/fibonacci/src/main.rs b/fibonacci/src/main.rs new file mode 100644 index 0000000..d4ae061 --- /dev/null +++ b/fibonacci/src/main.rs @@ -0,0 +1,52 @@ +extern crate separator; + +use separator::Separatable; + +use std::io; + +///! Calculate a number in the fibonnacci sequence, +///! using a lookup table for better worst-case performance. +/// +/// Can calculate up to 186 using native unsigned 128 bit integers. +fn fibonacci(n: usize, table: &mut Vec) -> u128 { + match table.get(n) { + Some(x) => *x, // Vec.get returns a Option with a reference to the value, so deref + None => { + let a = n - 1; + let b = n - 2; + + let current = fibonacci(a, table) + fibonacci(b, table); + + table.insert(n, current); + + current + } + } +} + +fn main() { + // The lookup table for previously calculated values + let mut table: Vec = vec![0, 1, 1, 2, 3, 5]; + + println!("Fibonacci calculator."); + println!("Any non-number will exist."); + + loop { + println!("\nWhich index in the sequence to calculate?"); + + let mut index = String::new(); + + io::stdin().read_line(&mut index) + .expect("Failed to read line"); + + let index = match index.trim().parse() { + Ok(num) => num, + Err(_) => break, // Exit on non-number + }; + + let calculated = fibonacci(index, &mut table); + + println!("Calculating number for index: {}", index); + println!("The Fibonnacci number at index: {} is {}", index, calculated.separated_string()); + } +}