Created
June 4, 2024 10:26
-
-
Save codecakes/4e83c19ac0220d4ab40eebd881caa0dd to your computer and use it in GitHub Desktop.
A linear recursive fibonacci series
This file contains hidden or 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
def fib(n): | |
# print(f"for fib {n}") | |
a, b = 0, 1 | |
if n == a: return a | |
if n == b: return b | |
return do_fib(n-1, b, a+b) | |
def do_fib(n, /, a=0, b=1): | |
if n == 0: return a | |
return do_fib(n-1, b, a+b) | |
assert fib(0) == 0 | |
assert(fib(1)) == 1 | |
assert(fib(2)) == 1 | |
assert(fib(3)) == 2 | |
assert(fib(4)) == 3 | |
assert(fib(5)) == 5 | |
assert(fib(6)) == 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment