Fibonacci sequence: Difference between revisions

Content deleted Content added
→‎{{header|Mathematica}} / {{header|Wolfram Language}}: simplification of more efficient recursive and iterative....
Line 4,759: Line 4,759:


<lang python>def fibFastRec(n):
<lang python>def fibFastRec(n):
if n < 2: return n
def fib(prvprv, prv, c):
if c < 1: return prvprv
else:
def fib(prvprv, prv, c):
else: return fib(prv, prvprv + prv, c - 1)
if c < 1: return (prvprv + prv)
return fib(0, 1, n)</lang>
else: return fib(prv, prvprv + prv, c - 1)
return fib(0, 1, n - 2)</lang>


However, although much faster and not requiring memory, the above code can only process to a limited 'n' due to the limit on stack recursion depth by Python; it is better to use the iterative approach above or the generative one below.
However, although much faster and not requiring memory, the above code can only process to a limited 'n' due to the limit on stack recursion depth by Python; it is better to use the iterative approach above or the generative one below.