Created getAllFib() variants

This commit is contained in:
2020-10-19 10:52:17 -04:00
parent c0ca08838d
commit fdfdb7bec1
2 changed files with 147 additions and 0 deletions

97
Algorithms.ts Normal file
View File

@@ -0,0 +1,97 @@
//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;
}

50
TestAlgorithms.ts Normal file
View File

@@ -0,0 +1,50 @@
//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/>.
*/
import assert = require("assert");
import { arrayEquals, getAllFib, getAllFibBig } from "./Algorithms";
function testGetAllFib(){
//Test 1
let correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
let highestNumber = 100;
let answer = getAllFib(highestNumber);
assert.ok(arrayEquals(answer, correctAnswer), "getAllFib Integer 1 failed");
//Test 2
correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
highestNumber = 1000;
answer = getAllFib(highestNumber);
assert.ok(arrayEquals(answer, correctAnswer), "getAllFib Integer 2 failed");
//Test 3
let bigCorrectAnswer = [BigInt(1), BigInt(1), BigInt(2), BigInt(3), BigInt(5), BigInt(8), BigInt(13), BigInt(21), BigInt(34), BigInt(55), BigInt(89)];
let bigHighestNumber = BigInt(100);
let bigAnswer = getAllFibBig(bigHighestNumber);
assert.ok(arrayEquals(bigAnswer, bigCorrectAnswer), "getAllFibBig failed");
console.log("testGetAllFib passed");
}
//Run all of the tests
testGetAllFib();