Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BreakOutStack(Exception):
- """Raised to clear stack frame and reset"""
- def __init__(self, *args):
- self.args = args
- def tail_recursive(fn):
- def _wrapped_tail_recursive(*args):
- while True:
- try:
- return fn(*args)
- except BreakOutStack as b:
- args = b.args
- return _wrapped_tail_recursive
- def recurse(*args):
- raise BreakOutStack(*args)
- @tail_recursive
- def factoral(n, a = 1):
- if n < 1:
- raise Exception("Bad Input")
- if n == 1:
- return a
- return recurse(n - 1, n * a)
- @tail_recursive
- def fibonnacci(n, a = 0, b = 1):
- if n < 0:
- raise Exception("Bad Input")
- if n == 0:
- return a
- if n == 1:
- return b
- return recurse(n - 1, b, a + b)
- n = 100000
- fn = fibonnacci
- print("fn({}) => {}".format(n, fn(n)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement