Skip to content

Instantly share code, notes, and snippets.

@zolotyh
Last active August 3, 2019 12:48
Show Gist options
  • Save zolotyh/88b8a32e9d527cd107a15c7b6de15852 to your computer and use it in GitHub Desktop.
Save zolotyh/88b8a32e9d527cd107a15c7b6de15852 to your computer and use it in GitHub Desktop.
void main(){
print(fib(5));
print(fibCached(100));
}
int fib(input) {
if (input == 0) {
return 0;
} else if (input == 1) {
return 1;
}
return fib(input - 1) + fib(input - 2);
}
int fibCached(input) {
int helper(int n, [a = 0, b = 1]) {
if (n == 0) {
return a;
} else {
return helper(n - 1, b, a + b);
}
}
if (input == 0) {
return 0;
} else {
return helper(input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment