Add recursive fibonnachi example
This commit is contained in:
parent
396dde530e
commit
497ca00d24
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -45,6 +45,7 @@
|
||||
<cargoProject FILE="$PROJECT_DIR$/pancakes/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/hello/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/old_lady_who_swallowed_a_fly/Cargo.toml" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/fibonacci/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
5
.idea/rust.iml
generated
5
.idea/rust.iml
generated
@ -181,6 +181,10 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/old_lady_who_swallowed_a_fly/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/old_lady_who_swallowed_a_fly/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/old_lady_who_swallowed_a_fly/benches" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/fibonacci/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/fibonacci/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/fibonacci/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/fibonacci/benches" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/add/add-one/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/add/adder/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/adder/target" />
|
||||
@ -194,6 +198,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/ctof/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/enums/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/errors/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/fibonacci/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/ftoc/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/functions/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/generics/target" />
|
||||
|
133
.idea/workspace.xml
generated
133
.idea/workspace.xml
generated
@ -14,11 +14,38 @@
|
||||
</component>
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/old_lady_who_swallowed_a_fly/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="768">
|
||||
<caret line="48" column="10" lean-forward="true" selection-start-line="48" selection-start-column="10" selection-end-line="48" selection-end-column="10" />
|
||||
<caret line="48" column="10" selection-start-line="48" selection-start-column="10" selection-end-line="48" selection-end-column="10" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/fibonacci/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="192">
|
||||
<caret line="12" column="93" selection-start-line="12" selection-start-column="93" selection-end-line="12" selection-end-column="93" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/fibonacci/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="112">
|
||||
<caret line="7" column="19" selection-start-line="7" selection-start-column="19" selection-end-line="7" selection-end-column="19" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/guessing_game/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
<caret line="9" selection-start-line="9" selection-end-line="9" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -56,12 +83,15 @@
|
||||
<find>expensive_closure(intensity)</find>
|
||||
<find>ordinals</find>
|
||||
<find>///</find>
|
||||
<find>u128</find>
|
||||
<find>u64</find>
|
||||
</findStrings>
|
||||
<replaceStrings>
|
||||
<replace>f_to_c</replace>
|
||||
<replace>expensive_result.value(intensity)</replace>
|
||||
<replace>ORDINALS</replace>
|
||||
<replace>//!</replace>
|
||||
<replace>u128</replace>
|
||||
</replaceStrings>
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
@ -70,8 +100,6 @@
|
||||
<component name="IdeDocumentHistory">
|
||||
<option name="CHANGED_PATHS">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/minigrep/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/closures/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/closures/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/minigrep_v2/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/minigrep_v2/src/lib.rs" />
|
||||
@ -121,6 +149,8 @@
|
||||
<option value="$PROJECT_DIR$/hello/src/lib.rs" />
|
||||
<option value="$PROJECT_DIR$/hello/src/bin/main.rs" />
|
||||
<option value="$PROJECT_DIR$/old_lady_who_swallowed_a_fly/src/main.rs" />
|
||||
<option value="$PROJECT_DIR$/fibonacci/Cargo.toml" />
|
||||
<option value="$PROJECT_DIR$/fibonacci/src/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -131,10 +161,10 @@
|
||||
<other-services-enabled>false</other-services-enabled>
|
||||
</component>
|
||||
<component name="ProjectFrameBounds" extendedState="6">
|
||||
<option name="x" value="1680" />
|
||||
<option name="y" value="370" />
|
||||
<option name="width" value="1050" />
|
||||
<option name="height" value="877" />
|
||||
<option name="x" value="1987" />
|
||||
<option name="y" value="-7" />
|
||||
<option name="width" value="1080" />
|
||||
<option name="height" value="1057" />
|
||||
</component>
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
|
||||
<ConfirmationsSetting value="1" id="Add" />
|
||||
@ -177,12 +207,23 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="loops" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="fibonacci" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="loops" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="fibonacci" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="guessing_game" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="guessing_game" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
@ -215,7 +256,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1549991319219" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1550777495454" />
|
||||
<property name="javascript.nodejs.core.library.configured.version" value="7.1.0" />
|
||||
<property name="js.eslint.eslintPackage" value="$USER_HOME$/.yarn-config/global/node_modules/.bin/eslint" />
|
||||
<property name="js.eslint.nodeInterpreter" value="project" />
|
||||
@ -249,7 +290,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run old_lady_who_swallowed_a_fly">
|
||||
<component name="RunManager" selected="Cargo Command.Run fibonacci">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -278,13 +319,13 @@
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="Run advanced_types" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<configuration name="Run fibonacci" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package advanced_types --bin advanced_types" />
|
||||
<option name="command" value="run --package fibonacci --bin fibonacci" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_types" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/fibonacci" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
@ -327,11 +368,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run fibonacci" />
|
||||
<item itemvalue="Cargo Command.Run old_lady_who_swallowed_a_fly" />
|
||||
<item itemvalue="Cargo Command.Run hello" />
|
||||
<item itemvalue="Cargo Command.Run pancakes" />
|
||||
<item itemvalue="Cargo Command.Run advanced_functions" />
|
||||
<item itemvalue="Cargo Command.Run advanced_types" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -368,11 +409,11 @@
|
||||
</history-entry>
|
||||
</component>
|
||||
<component name="ToolWindowManager">
|
||||
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
|
||||
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
|
||||
<editor active="true" />
|
||||
<layout>
|
||||
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.49522156" visible="true" weight="0.26141885" />
|
||||
<window_info id="Structure" order="1" sideWeight="0.50477844" side_tool="true" visible="true" weight="0.26141885" />
|
||||
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.49524873" visible="true" weight="0.2540132" />
|
||||
<window_info id="Structure" order="1" sideWeight="0.50475127" side_tool="true" visible="true" weight="0.2540132" />
|
||||
<window_info id="Image Layers" order="2" />
|
||||
<window_info id="Designer" order="3" />
|
||||
<window_info id="Capture Tool" order="4" />
|
||||
@ -380,7 +421,7 @@
|
||||
<window_info id="Cargo" order="6" sideWeight="0.49791494" weight="0.3286119" />
|
||||
<window_info anchor="bottom" id="Message" order="0" />
|
||||
<window_info anchor="bottom" id="Find" order="1" weight="0.32979318" />
|
||||
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.25693995" />
|
||||
<window_info anchor="bottom" id="Run" order="2" weight="0.22247066" />
|
||||
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
|
||||
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
|
||||
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
|
||||
@ -402,27 +443,6 @@
|
||||
<option name="version" value="1" />
|
||||
</component>
|
||||
<component name="editorHistoryManager">
|
||||
<entry file="file://$PROJECT_DIR$/minigrep/poem.txt">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="128">
|
||||
<caret line="8" column="19" selection-start-line="8" selection-start-column="19" selection-end-line="8" selection-end-column="19" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/minigrep/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="517">
|
||||
<caret line="33" column="6" selection-start-line="33" selection-start-column="6" selection-end-line="33" selection-end-column="6" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/minigrep/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="320">
|
||||
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/closures/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="1120">
|
||||
@ -715,9 +735,7 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/macros/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor" />
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/macros/src/main.rs" />
|
||||
<entry file="file://$PROJECT_DIR$/hello_macro/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="112">
|
||||
@ -803,6 +821,13 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/old_lady_who_swallowed_a_fly/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="768">
|
||||
<caret line="48" column="10" selection-start-line="48" selection-start-column="10" selection-end-line="48" selection-end-column="10" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/twelve_days_of_christmas/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="752">
|
||||
@ -810,6 +835,13 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/guessing_game/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="144">
|
||||
<caret line="9" selection-start-line="9" selection-end-line="9" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/loops/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="944">
|
||||
@ -817,10 +849,17 @@
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/old_lady_who_swallowed_a_fly/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/fibonacci/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="768">
|
||||
<caret line="48" column="10" lean-forward="true" selection-start-line="48" selection-start-column="10" selection-end-line="48" selection-end-column="10" />
|
||||
<state relative-caret-position="112">
|
||||
<caret line="7" column="19" selection-start-line="7" selection-start-column="19" selection-end-line="7" selection-end-column="19" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/fibonacci/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="192">
|
||||
<caret line="12" column="93" selection-start-line="12" selection-start-column="93" selection-end-line="12" selection-end-column="93" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
8
fibonacci/Cargo.toml
Normal file
8
fibonacci/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "fibonacci"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
separator = "0.4.0"
|
52
fibonacci/src/main.rs
Normal file
52
fibonacci/src/main.rs
Normal file
@ -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>) -> u128 {
|
||||
match table.get(n) {
|
||||
Some(x) => *x, // Vec<T>.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<u128> = 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user