mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Added a function to get a specific Fibonacci number
This commit is contained in:
@@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user