From 48b731f8fa5686db47c1184e34d5e2005da2dcd3 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 26 Feb 2019 13:52:31 -0500 Subject: [PATCH] Check for overflows in fibonacci calculator --- .idea/misc.xml | 6 + .idea/rust.iml | 30 ++ .idea/workspace.xml | 366 +++++++++--------- .../fibonacci/src/main.rs | 33 +- 4 files changed, 236 insertions(+), 199 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fc4f7d3..bbd63f7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -47,6 +47,12 @@ + + + + + + diff --git a/.idea/rust.iml b/.idea/rust.iml index 42a672f..59fd6f5 100644 --- a/.idea/rust.iml +++ b/.idea/rust.iml @@ -189,6 +189,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 19b7c83..fd96edf 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,18 +2,10 @@ - + + - - - - - - - - - - + - + + + + + + + + + + + + + + + + + + + + - @@ -128,6 +139,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - + @@ -200,7 +229,7 @@ - + + + - - + - @@ -319,10 +348,11 @@ - + + - - + + @@ -330,7 +360,7 @@ - + @@ -346,88 +376,13 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -449,26 +404,7 @@ - - - - - - - - - - - - - - - - - - - - + @@ -494,19 +430,6 @@ - - - - - - - - - - - - - @@ -514,13 +437,6 @@ - - - - - - - @@ -528,13 +444,6 @@ - - - - - - - @@ -680,17 +589,6 @@ - - - - - - - - - - - @@ -698,13 +596,7 @@ - - - - - - - + @@ -731,10 +623,106 @@ - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/__non-book-expirements__/fibonacci/src/main.rs b/__non-book-expirements__/fibonacci/src/main.rs index 7bdc046..84d5fef 100644 --- a/__non-book-expirements__/fibonacci/src/main.rs +++ b/__non-book-expirements__/fibonacci/src/main.rs @@ -3,23 +3,29 @@ 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 186 using native unsigned 128 bit integers. -fn fibonacci(n: usize, table: &mut Vec) -> u128 { +fn fibonacci(n: usize, table: &mut Vec) -> Option { match table.get(n) { - Some(x) => *x, // Vec.get returns a Option with a reference to the value, so deref + Some(x) => Some(*x), // Vec.get returns a Option with a reference to the value, so deref None => { - let a = n - 1; - let b = n - 2; + let a = fibonacci(n - 1, table) + .unwrap_or(u128::MAX); + let b = fibonacci(n - 2, table) + .unwrap_or(u128::MAX); - let current = fibonacci(a, table) + fibonacci(b, table); + // Check for overflow when adding + let attempt = a.checked_add(b); - table.insert(n, current); + if let Some(current) = attempt { + table.insert(n, current); + } - current + attempt } } } @@ -44,9 +50,16 @@ fn main() { 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()); + + match fibonacci(index, &mut table) { + Some(calculated) => { + println!("The Fibonacci number at index: {} is {}", index, calculated.separated_string()); + }, + None => { + println!("Calculation overflow. The factorial base was too large."); + } + } + } }