Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created June 4, 2024 10:26
Show Gist options
  • Save codecakes/4e83c19ac0220d4ab40eebd881caa0dd to your computer and use it in GitHub Desktop.
Save codecakes/4e83c19ac0220d4ab40eebd881caa0dd to your computer and use it in GitHub Desktop.
A linear recursive fibonacci series
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