Skip to content

Instantly share code, notes, and snippets.

@aershov24
Created October 13, 2020 05:28
Show Gist options
  • Select an option

  • Save aershov24/b89a526665dfe1781a3b10084a8a2829 to your computer and use it in GitHub Desktop.

Select an option

Save aershov24/b89a526665dfe1781a3b10084a8a2829 to your computer and use it in GitHub Desktop.
Markdium-12 Recursion Interview Questions (SOLVED) Devs Have To Nail
private static final int FIB_0 = 0;
private static final int FIB_1 = 1;
private int calcFibonacci(final int target) {
if (target == 0) { return FIB_0; }
if (target == 1) { return FIB_1; }
return calcFibonacci(target, 1, FIB_1, FIB_0);
}
private int calcFibonacci(final int target, final int previous, final int fibPrevious, final int fibPreviousMinusOne) {
final int current = previous + 1;
final int fibCurrent = fibPrevious + fibPreviousMinusOne;
// If you want, print here / memoize for future calls
if (target == current) { return fibCurrent; }
return calcFibonacci(target, current, fibCurrent, fibPrevious);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment