Files
TypescriptClasses/Algorithms.ts

98 lines
2.8 KiB
TypeScript

//typescriptClasses/Algorithms.ts
//Matthew Ellison
// Created: 10-19-20
//Modified: 10-19-20
//This class holds many algorithms that I have found it useful to keep around
/*
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export function arrayEquals(array1: any[], array2: any[]): boolean{
//If they aren't the same type they aren't equal
if((typeof array1) != (typeof array2)){
return false;
}
//If they aren't the same length they aren't equal
else if(array1.length != array2.length){
return false;
}
else{
//Loop through every element to see if each one is equal
for(let cnt = 0;cnt < array1.length;++cnt){
//If any element in the same location is different return false
if(array1[cnt] != array2[cnt]){
return false;
}
}
//If every element was the same they are equal
return true;
}
}
export function getAllFib(goalNumber: number): number[]{
//Setup the variables
let fibNums: number[] = [];
//If the number is <= 0 return an empty list
if(goalNumber <= 0){
return fibNums;
}
else if(goalNumber == 1){
fibNums.push(1);
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.length - 1] <= goalNumber){
fibNums.push((fibNums[fibNums.length - 1]) + (fibNums[fibNums.length - 2]));
}
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
fibNums.pop();
return fibNums;
}
export function getAllFibBig(goalNumber: bigint): bigint[]{
//Setup the variables
let fibNums:bigint[] = [];
//If the number is <= 0 return an empty list
if(goalNumber <= 0){
return fibNums;
}
else if(goalNumber == BigInt(1)){
fibNums.push(BigInt(1));
return fibNums;
}
//This means that at least 2 1's are elements
fibNums.push(BigInt(1));
fibNums.push(BigInt(1));
//Loop to generate the rest of the Fibonacci numbers
while(fibNums[fibNums.length - 1] <= goalNumber){
fibNums.push((fibNums[fibNums.length - 1]) + (fibNums[fibNums.length - 2]));
}
//At this point the most recent number is > goalNumber, so remove it and return the rest of the list
fibNums.pop();
return fibNums;
}