Last active
August 3, 2019 12:48
-
-
Save zolotyh/88b8a32e9d527cd107a15c7b6de15852 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
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