This page documents library functions related to sequences.
Fibonacci
gfib(n) Generate the Fibonacci sequence up to its n-th term.
Arguments
| Argument | Description | Domain | Constraints | Notes |
|---|
| n | terms count | Z | 0<n | |
Return
| Value | Description | Codomain | Constraints | Notes |
|---|
| ω | terms | [N,...] | | Produces an empty list for invalid n. |
Instances
g_\text{fib}left(0\right)-left[\right]
g_\text{fib}left(1\right)-left[0\right]
g_\text{fib}left(2\right)-left[0, 1\right]
g_\text{fib}left(3\right)-left[0, 1, 1\right]
g_\text{fib}left(7\right)-left[0, 1, 1, 2, 3, 5, 8\right]
g_\text{fib}left(13\right)-left[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144\right]
Implementation
gfib(n)gfibs(n, nmax)gfibo(n, nmax)={ n≤0: [], n=1: [0], n=2: [0, 1], join([0, 1], gfibo(1, n−2)) }=join([0, 1], gfibo(n+1, nmax))={n>nmax: [], gfibs(n, nmax)+ftail(gfibs(n, nmax))} This uses a mutually recursive implementation with 2 helper functions. It could probably be optimised?
Dependencies
- ftail (from Lists)