Skip to content

Instantly share code, notes, and snippets.

@TomaQ
Created February 21, 2019 05:26
Show Gist options
  • Save TomaQ/7d28c3eb40911ea2506ccc3cb8f7563a to your computer and use it in GitHub Desktop.
Save TomaQ/7d28c3eb40911ea2506ccc3cb8f7563a to your computer and use it in GitHub Desktop.
// 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