Add a factorial example, checking for integer overflow
This commit is contained in:
parent
b879914688
commit
9c2b4da36e
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
@ -46,6 +46,7 @@
|
||||
<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" />
|
||||
<cargoProject FILE="$PROJECT_DIR$/factorial/Cargo.toml" />
|
||||
</component>
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
|
5
.idea/rust.iml
generated
5
.idea/rust.iml
generated
@ -185,6 +185,10 @@
|
||||
<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" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/factorial/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/factorial/examples" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/factorial/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/factorial/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" />
|
||||
@ -198,6 +202,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$/factorial/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/fibonacci/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/ftoc/target" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/functions/target" />
|
||||
|
166
.idea/workspace.xml
generated
166
.idea/workspace.xml
generated
@ -2,7 +2,9 @@
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="c8f42924-1cd2-4b1c-bcff-602a3368bb16" name="Default Changelist" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/fibonacci/src/main.rs" beforeDir="false" afterPath="$PROJECT_DIR$/fibonacci/src/main.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/rust.iml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/rust.iml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
</list>
|
||||
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
@ -13,19 +15,10 @@
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
|
||||
<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" 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="496">
|
||||
<caret line="31" column="37" selection-start-line="31" selection-start-column="37" selection-end-line="31" selection-end-column="37" />
|
||||
<state relative-caret-position="96">
|
||||
<caret line="6" selection-start-line="6" selection-end-line="6" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -33,35 +26,35 @@
|
||||
<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 relative-caret-position="128">
|
||||
<caret line="8" selection-start-line="8" selection-end-line="8" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/factorial/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="885">
|
||||
<caret line="65" column="26" lean-forward="true" selection-start-line="65" selection-start-column="26" selection-end-line="65" selection-end-column="26" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/guessing_game/src/main.rs">
|
||||
<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="144">
|
||||
<caret line="9" selection-start-line="9" selection-end-line="9" />
|
||||
<state relative-caret-position="375">
|
||||
<caret line="35" column="12" selection-start-line="35" selection-start-column="12" selection-end-line="35" selection-end-column="12" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<entry file="file://$PROJECT_DIR$/loops/src/main.rs">
|
||||
<entry file="file://$PROJECT_DIR$/factorial/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="944">
|
||||
<caret line="59" selection-start-line="59" selection-end-line="59" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
</file>
|
||||
<file pinned="false" current-in-tab="false">
|
||||
<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">
|
||||
<caret line="47" selection-start-line="47" selection-end-line="47" />
|
||||
<state relative-caret-position="128">
|
||||
<caret line="8" selection-start-line="8" selection-end-line="8" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
@ -98,8 +91,6 @@
|
||||
<component name="IdeDocumentHistory">
|
||||
<option name="CHANGED_PATHS">
|
||||
<list>
|
||||
<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" />
|
||||
<option value="$PROJECT_DIR$/add/Cargo.toml" />
|
||||
<option value="$PROJECT_DIR$/add/adder/src/main.rs" />
|
||||
@ -149,6 +140,8 @@
|
||||
<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" />
|
||||
<option value="$PROJECT_DIR$/factorial/Cargo.toml" />
|
||||
<option value="$PROJECT_DIR$/factorial/src/main.rs" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
@ -158,11 +151,11 @@
|
||||
<annotation-enable>false</annotation-enable>
|
||||
<other-services-enabled>false</other-services-enabled>
|
||||
</component>
|
||||
<component name="ProjectFrameBounds" extendedState="6">
|
||||
<option name="x" value="1987" />
|
||||
<option name="y" value="-7" />
|
||||
<option name="width" value="1080" />
|
||||
<option name="height" value="1057" />
|
||||
<component name="ProjectFrameBounds">
|
||||
<option name="x" value="1680" />
|
||||
<option name="y" value="-410" />
|
||||
<option name="width" value="1050" />
|
||||
<option name="height" value="1657" />
|
||||
</component>
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
|
||||
<ConfirmationsSetting value="1" id="Add" />
|
||||
@ -172,6 +165,7 @@
|
||||
<foldersAlwaysOnTop value="true" />
|
||||
</navigator>
|
||||
<panes>
|
||||
<pane id="PackagesPane" />
|
||||
<pane id="ProjectPane">
|
||||
<subPane>
|
||||
<expand>
|
||||
@ -194,7 +188,6 @@
|
||||
<select />
|
||||
</subPane>
|
||||
</pane>
|
||||
<pane id="PackagesPane" />
|
||||
<pane id="Scope">
|
||||
<subPane subId="Scope 'Project Files'; set:Project Files; class com.intellij.psi.search.scope.ProjectFilesScope">
|
||||
<expand>
|
||||
@ -205,13 +198,18 @@
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="fibonacci" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="factorial" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
<item name="rust" type="2674bda8:ScopeViewTreeModel$GroupNode" />
|
||||
<item name="factorial" 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="fibonacci" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
<item name="src" type="9f88c78c:ScopeViewTreeModel$FileNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="rust" type="3d21c010:ScopeViewTreeModel$ProjectNode" />
|
||||
@ -254,7 +252,7 @@
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="JavaScriptWeakerCompletionTypeGuess" value="true" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1550779908787" />
|
||||
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1550853600931" />
|
||||
<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" />
|
||||
@ -288,7 +286,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager" selected="Cargo Command.Run fibonacci">
|
||||
<component name="RunManager" selected="Cargo Command.Run factorial">
|
||||
<configuration name="<template>" type="TestNG" default="true" selected="false">
|
||||
<option name="MAIN_CLASS_NAME" />
|
||||
<option name="VM_PARAMETERS" value="-ea" />
|
||||
@ -307,13 +305,13 @@
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration name="Run advanced_functions" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<configuration name="Run factorial" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="command" value="run --package advanced_functions --bin advanced_functions" />
|
||||
<option name="command" value="run --package factorial --bin factorial" />
|
||||
<option name="allFeatures" value="false" />
|
||||
<option name="nocapture" value="false" />
|
||||
<option name="backtrace" value="SHORT" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/advanced_functions" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$/factorial" />
|
||||
<envs />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
@ -366,11 +364,11 @@
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Cargo Command.Run factorial" />
|
||||
<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" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
@ -407,11 +405,11 @@
|
||||
</history-entry>
|
||||
</component>
|
||||
<component name="ToolWindowManager">
|
||||
<frame x="1920" y="-438" width="1080" height="1897" extended-state="6" />
|
||||
<frame x="1680" y="-410" width="1050" height="1657" extended-state="6" />
|
||||
<editor active="true" />
|
||||
<layout>
|
||||
<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 content_ui="combo" id="Project" order="0" sideWeight="0.4946058" visible="true" weight="0.26141885" />
|
||||
<window_info id="Structure" order="1" sideWeight="0.5053942" side_tool="true" visible="true" weight="0.26141885" />
|
||||
<window_info id="Image Layers" order="2" />
|
||||
<window_info id="Designer" order="3" />
|
||||
<window_info id="Capture Tool" order="4" />
|
||||
@ -419,7 +417,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 anchor="bottom" id="Run" order="2" weight="0.22247066" />
|
||||
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.22207876" />
|
||||
<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" />
|
||||
@ -427,7 +425,7 @@
|
||||
<window_info anchor="bottom" id="Docker" order="7" show_stripe_button="false" />
|
||||
<window_info anchor="bottom" id="Version Control" order="8" />
|
||||
<window_info anchor="bottom" id="Inspection Results" order="9" weight="0.32979318" />
|
||||
<window_info anchor="bottom" id="Terminal" order="10" weight="0.34264952" />
|
||||
<window_info anchor="bottom" id="Terminal" order="10" weight="0.34215623" />
|
||||
<window_info anchor="bottom" id="Event Log" order="11" side_tool="true" />
|
||||
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
|
||||
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
|
||||
@ -441,24 +439,6 @@
|
||||
<option name="version" value="1" />
|
||||
</component>
|
||||
<component name="editorHistoryManager">
|
||||
<entry file="file://$PROJECT_DIR$/closures/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="1120">
|
||||
<caret line="70" column="1" selection-end-line="71" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/closures/src/lib.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="624">
|
||||
<caret line="39" column="57" selection-start-line="39" selection-start-column="57" selection-end-line="39" selection-end-column="57" />
|
||||
<folding>
|
||||
<element signature="e#437#438#0" expanded="true" />
|
||||
<element signature="e#472#473#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/minigrep_v2/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="112">
|
||||
@ -819,13 +799,6 @@
|
||||
</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">
|
||||
@ -833,13 +806,6 @@
|
||||
</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">
|
||||
@ -847,17 +813,45 @@
|
||||
</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="496">
|
||||
<caret line="31" column="8" selection-start-line="31" selection-start-column="8" selection-end-line="31" selection-end-column="8" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<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 relative-caret-position="128">
|
||||
<caret line="8" selection-start-line="8" selection-end-line="8" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/factorial/Cargo.toml">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="128">
|
||||
<caret line="8" selection-start-line="8" selection-end-line="8" />
|
||||
</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="375">
|
||||
<caret line="35" column="12" selection-start-line="35" selection-start-column="12" selection-end-line="35" selection-end-column="12" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/fibonacci/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="496">
|
||||
<caret line="31" column="37" selection-start-line="31" selection-start-column="37" selection-end-line="31" selection-end-column="37" />
|
||||
<state relative-caret-position="96">
|
||||
<caret line="6" selection-start-line="6" selection-end-line="6" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/factorial/src/main.rs">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="885">
|
||||
<caret line="65" column="26" lean-forward="true" selection-start-line="65" selection-start-column="26" selection-end-line="65" selection-end-column="26" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
|
8
factorial/Cargo.toml
Normal file
8
factorial/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "factorial"
|
||||
version = "0.1.0"
|
||||
authors = ["Timothy Warren <twarren@nabancard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
separator = "0.4.0"
|
73
factorial/src/main.rs
Normal file
73
factorial/src/main.rs
Normal file
@ -0,0 +1,73 @@
|
||||
extern crate separator;
|
||||
|
||||
use separator::Separatable;
|
||||
|
||||
use std::io;
|
||||
use std::u128;
|
||||
|
||||
///! Calculate a number in the fibonnacci sequence,
|
||||
///! using a lookup table for better worst-case performance.
|
||||
///
|
||||
/// Can calculate up to 34! using native unsigned 128 bit integers.
|
||||
/// If the result overflows, an error message will be displayed.
|
||||
fn factorial (n: usize, table: &mut Vec<u128>) -> Option<u128> {
|
||||
match table.get(n) {
|
||||
Some(x) => {
|
||||
// Vec<T>.get returns a Option with a reference to the value, so deref
|
||||
// Wrap in Some() for proper return type
|
||||
Some(*x)
|
||||
},
|
||||
None => {
|
||||
// If a previous base overflowed, just
|
||||
// pass on the overflow. The overflow will
|
||||
// be caught later. Using the max value of u128
|
||||
// for convenience.
|
||||
let prev = factorial(n - 1, table)
|
||||
.unwrap_or(u128::MAX);
|
||||
|
||||
// Do an overflow-checked multiply
|
||||
let attempt = (n as u128).checked_mul(prev);
|
||||
|
||||
// If there isn't an overflow, add the result
|
||||
// to the calculation table
|
||||
if let Some(current) = attempt {
|
||||
table.insert(n, current);
|
||||
}
|
||||
|
||||
attempt // Some(x) if no overflow
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// The lookup table for previously calculated values
|
||||
let mut table: Vec<u128> = vec![1, 1, 2];
|
||||
|
||||
println!("Factorial calculator.");
|
||||
println!("Any non-number will exit.");
|
||||
|
||||
loop {
|
||||
println!("\nWhich factorial 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
|
||||
};
|
||||
|
||||
println!("Calculating factorial of: {}", index);
|
||||
|
||||
match factorial(index, &mut table) {
|
||||
Some(calculated) => {
|
||||
println!("{}! is {}", index, calculated.separated_string())
|
||||
},
|
||||
None => {
|
||||
println!("Calculation overflow. The factorial base was too large.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user