From 523a0cad652aba5c764cc770d9def5d057e3705d Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 17 Jun 2020 18:15:22 -0400 Subject: [PATCH] Added function for getting a specific fibonacci num --- src/Algorithms.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/Algorithms.rs b/src/Algorithms.rs index ef9b5e5..a87656c 100644 --- a/src/Algorithms.rs +++ b/src/Algorithms.rs @@ -39,6 +39,44 @@ pub fn getAllFibBig(goalNumber: num::BigInt) -> Vec{ fibNums.remove(fibNums.len() - 1); return fibNums; } +//This function returns all the divisors of goalSubscript +pub fn getFib(goalSubscript: i64) -> i64{ + //Setup the variables + let mut fibNums = [1, 1, 0]; + + //If the number is <= 0 return 0 + if(goalSubscript <= 0){ + return 0; + } + + //Loop through the list, generating Fibonacci numbers until it finds the correct subscript + let mut fibLoc = 2; + while(fibLoc < goalSubscript){ + fibNums[(fibLoc % 3) as usize] = fibNums[((fibLoc - 1) % 3) as usize] + fibNums[((fibLoc - 2) % 3) as usize]; + fibLoc += 1; + } + //Return the proper number. The location counter is 1 off of the subscript + return fibNums[((fibLoc - 1) % 3) as usize]; +} +pub fn getFibBig(goalSubscript: num::BigInt) -> num::BigInt{ + //Setup the variables + let mut fibNums = [num::BigInt::from(1), num::BigInt::from(1), num::BigInt::from(0)]; + + //If the number is <= 0 return 0 + if(goalSubscript <= num::BigInt::from(0)){ + return num::BigInt::from(0); + } + + //Loop through the list, generating Fibonacci numbers until it finds the correct subscript + let mut fibLoc = 2; + while(num::BigInt::from(fibLoc) < goalSubscript){ + fibNums[(fibLoc % 3) as usize] = &fibNums[((fibLoc - 1) % 3) as usize] + &fibNums[((fibLoc - 2) % 3) as usize]; + fibLoc += 1; + } + //Return the proper number. The location counter is 1 off of the subscript + let temp = &fibNums[((fibLoc - 1) % 3) as usize]; + return num::BigInt::new(temp.to_u32_digits().0, temp.to_u32_digits().1); +} //This function returns all factors of goalNumber pub fn getFactors(mut goalNumber: i64) -> Vec{ diff --git a/src/lib.rs b/src/lib.rs index 2641285..3c56dc6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,25 @@ mod AlgorithmsTests{ assert_eq!(correctAnswerBig, answerBig); } #[test] + fn testGetFib(){ + //Test1 + let correctAnswer1 = 144; + let number1 = 12; + let answer1 = super::Algorithms::getFib(number1); + assert_eq!(correctAnswer1, answer1); + //Test2 + let correctAnswer2 = 6765; + let number2 = 20; + let answer2 = super::Algorithms::getFib(number2); + assert_eq!(correctAnswer2, answer2); + + //Test3 + let correctAnswer3 = "1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816".parse::().unwrap(); + let number3 = num::BigInt::from(4782); + let answer3 = super::Algorithms::getFibBig(number3); + assert_eq!(correctAnswer3, answer3); + } + #[test] fn testGetPrimes(){ //Test1 let mut correctAnswer1 = Vec::::new();