Skip to content

Instantly share code, notes, and snippets.

@myyc
Created November 1, 2016 14:56
Show Gist options
  • Save myyc/4ef2ddd897b38fd56710872034e3a307 to your computer and use it in GitHub Desktop.
Save myyc/4ef2ddd897b38fd56710872034e3a307 to your computer and use it in GitHub Desktop.
from functools import wraps
def c(f):
@wraps(f)
def w(k):
if k in d:
return d[k]
else:
d[k] = f(k)
return d[k]
return w
@c
def f(k):
"""recursive fibonacci"""
if k == 1 or k == 2:
return 1
elif k > 2 :
return f(k-1) + f(k-2)
else:
raise ValueError("k > 0")
def f(k):
"""non-recursive fibonacci"""
a1 = 1
a2 = 1
if k == 1 or k == 2:
return a1
elif k > 2:
for i in range(3, k+1):
s = a2 + a1
a2 = a1
a1 = s
return a1
else:
raise ValueError("k > 0")
def cf(int k):
"""cythonised non-recursive fibonacci"""
cdef int a1 = 1
cdef int a2 = 1
cdef int i, s
if k == 1 or k == 2:
return a1
elif k > 2:
for i in range(3, k+1):
s = a2 + a1
a2 = a1
a1 = s
return a1
else:
raise ValueError("k > 0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment