Created
February 21, 2019 05:26
-
-
Save TomaQ/7d28c3eb40911ea2506ccc3cb8f7563a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1a | |
public int FibCalcSum(int fib) | |
{ | |
if(fib < 0) | |
throw new ArgumentException("Fib number must be greater than or equal to 0"); | |
if(fib < 2) | |
return fib; | |
return FibCalcSum(fib - 1) + FibCalcSum(fib - 2); | |
} | |
// 1b | |
// Fuck no loops cause only people that memorized the fib equation would know the answer | |
// so here's the answer that a normal interviewer would accept | |
public int FibCalcSum(int fib) | |
{ | |
if(fib < 0) | |
throw new ArgumentException("Fib number must be greater than or equal to 0"); | |
if(fib < 2) | |
return fib; | |
int prevPrevNum = 0; | |
int prevNum = 1; | |
int currentNum = prevPrevNum + prevNum; | |
for(int i = 2; i <= fib; i++) | |
{ | |
currentNum = prevPrevNum + prevNum; | |
prevPrevNum = prevNum; | |
prevNum = currentNum; | |
} | |
return currentNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment