mirror of
https://bitbucket.org/Mattrixwv/rustclasses.git
synced 2025-12-07 02:43:59 -05:00
Initial commit with a few functions
This commit is contained in:
41
src/Algorithms.rs
Normal file
41
src/Algorithms.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
extern crate num;
|
||||
|
||||
|
||||
pub fn getAllFib(goalNumber: u64) -> Vec<u64>{
|
||||
let mut fibNums = Vec::new(); //A list to save the Fibonacci numbers
|
||||
//If the number is <= 0 return an empty list
|
||||
if(goalNumber <= 0){
|
||||
return fibNums;
|
||||
}
|
||||
|
||||
//This means that at least 2 1's are elements
|
||||
fibNums.push(1);
|
||||
fibNums.push(1);
|
||||
|
||||
//Loop to generate the rest of the Fibonacci numbers
|
||||
while(fibNums[fibNums.len() - 1] <= goalNumber){
|
||||
fibNums.push(fibNums[fibNums.len() - 1] + fibNums[fibNums.len() - 2]);
|
||||
}
|
||||
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
|
||||
fibNums.remove(fibNums.len() - 1);
|
||||
return fibNums;
|
||||
}
|
||||
|
||||
pub fn getAllFibBig(goalNumber: num::BigInt) -> Vec<num::BigInt>{
|
||||
let mut fibNums = Vec::new(); //A list to save the Fibonacci numbers in
|
||||
//If the number is <= 0 return an empty list
|
||||
if(goalNumber <= num::BigInt::from(0)){
|
||||
return fibNums;
|
||||
}
|
||||
|
||||
//This means that at least 2 1's are elements
|
||||
fibNums.push(num::BigInt::from(1));
|
||||
fibNums.push(num::BigInt::from(1));
|
||||
|
||||
while(fibNums[fibNums.len() - 1] <= goalNumber){
|
||||
fibNums.push(&fibNums[fibNums.len() - 1] + &fibNums[fibNums.len() - 2]);
|
||||
}
|
||||
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
|
||||
fibNums.remove(fibNums.len() - 1);
|
||||
return fibNums;
|
||||
}
|
||||
Reference in New Issue
Block a user