From 3eb91a703bd59f901fc0122ffe51b5ed41c105d9 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 8 Apr 2021 13:43:10 -0400 Subject: [PATCH] Added function to get a specific fibbonacci number --- Algorithms.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Algorithms.ts b/Algorithms.ts index b25ac0c..4b5f867 100644 --- a/Algorithms.ts +++ b/Algorithms.ts @@ -498,6 +498,43 @@ export function getProdBig(ary: bigint[]): bigint{ return prod; } +export function getFib(goalSubscript: number): number{ + //Setup the variables + let fibNums: number[] = [1, 1, 0]; //A list to keep track of the Fibonacci numbers. It need only be 3 long because we only need the one we are working on and the last 2 + + //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 fibLoc: number = 2; + for(fibLoc = 2;fibLoc < goalSubscript;++fibLoc){ + fibNums[fibLoc % 3] = fibNums[(fibLoc - 1) % 3] + fibNums[(fibLoc - 2) % 3]; + } + + //Return the proper number. The location counter is 1 off of the subscript + return fibNums[(fibLoc - 1) % 3]; +} +export function getFibBig(goalSubscript: bigint): bigint{ + //Setup the varibles + let fibNums: bigint[] = [1n, 1n, 0n]; //A list to keep track of the Fibonacci numbers. It need only be 3 long because we only need the one we are working on and the last 2 + + //If the number is <= 0 return 0 + if(goalSubscript <= 0n){ + return 0n; + } + + //Loop through the list, generating Fibonacci numbers until it finds the correct subscript + let fibLoc: bigint = 2n; + for(fibLoc = 2n;fibLoc < goalSubscript;++fibLoc){ + fibNums[Number(fibLoc % 3n)] = fibNums[Number((fibLoc - 1n) % 3n)] + fibNums[Number((fibLoc - 2n) % 3n)]; + } + + //Return the proper number. The location counter is 1 off of the subscript + return fibNums[Number((fibLoc - 1n) % 3n)]; +} + //This is a function that creates all permutations of a string and returns a vector of those permutations export function getPermutations(master: string): string[]{ return getPermutationsHelper(master, 0);