Jump to content

Fibonacci sequence: Difference between revisions

(→‎{{header|Mathematica}} / {{header|Wolfram Language}}: simplification of more efficient recursive and iterative....)
Line 4,759:
 
<lang python>def fibFastRec(n):
ifdef nfib(prvprv, <prv, 2c): return n
if c < else1: return fib(prv, prvprv + prv, c - 1)
else:
defelse: return fib(prvprvprv, prvprv + prv, c - 1):
if c < 1: return fib(prvprv0, +1, prvn)</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.
474

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.