-
-
Save myyc/4ef2ddd897b38fd56710872034e3a307 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
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