Fibonacci sequence: Difference between revisions
Content added Content deleted
(→{{header|Logo}}: +Mathematica) |
|||
Line 345: | Line 345: | ||
output fib_acc :n 0 1 |
output fib_acc :n 0 1 |
||
end |
end |
||
=={{header|Mathematica}}== |
|||
Mathematica already has a built-in function <code>Fibonacci</code>, but a simple recursive implementation would be |
|||
fib[0] = 0 |
|||
fib[1] = 1 |
|||
fib[n_Integer] := fib[n - 1] + fib[n - 2] |
|||
An optimization is to cache the values already calculated: |
|||
fib[0] = 0 |
|||
fib[1] = 1 |
|||
fib[n_Integer] := fib[n] = fib[n - 1] + fib[n - 2] |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |