diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index de669cc..d7031c4 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -650,5 +650,60 @@ namespace mee{ tempStr[second] = temp; return new string(tempStr); } + //This function returns the goalSubscript'th Fibonacci number + public static int GetFib(int goalSubscript){ + //Setup the variables + int[] fibNums = {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 + int fibLoc; + 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]; + } + public static long GetFib(long goalSubscript){ + //Setup the variables + long[] fibNums = {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 + long fibLoc; + 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]; + } + public static BigInteger GetFib(BigInteger goalSubscript){ + //Setup the variables + BigInteger[] fibNums = {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 + long fibLoc; + 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]; + } } }