Fibonacci sequence: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring the hard way) |
(→Excel LAMBDA: Added a variant, obtaining just the Nth term, defined in terms of a fold.) |
||
Line 4,122: | Line 4,122: | ||
| 377 |
| 377 |
||
| 610 |
| 610 |
||
|} |
|||
Or as a fold, obtaining just the Nth term of the Fibonacci series: |
|||
<lang lisp>FIBONACCI2 |
|||
=LAMBDA(n, |
|||
INDEX( |
|||
FOLDL( |
|||
LAMBDA(ab, |
|||
LAMBDA(_, |
|||
APPEND(INDEX(ab, 2))(SUM(ab)) |
|||
) |
|||
) |
|||
)({0;1})( |
|||
ENUMFROMTO(1)(n) |
|||
), |
|||
1 |
|||
) |
|||
)</lang> |
|||
Assuming the following generic bindings in the Excel worksheet Name manager: |
|||
<lang lisp>APPEND |
|||
=LAMBDA(xs, |
|||
LAMBDA(ys, |
|||
LET( |
|||
nx, ROWS(xs), |
|||
rowIndexes, SEQUENCE(nx + ROWS(ys)), |
|||
colIndexes, SEQUENCE( |
|||
1, |
|||
MAX(COLUMNS(xs), COLUMNS(ys)) |
|||
), |
|||
IF( |
|||
rowIndexes <= nx, |
|||
INDEX(xs, rowIndexes, colIndexes), |
|||
INDEX(ys, rowIndexes - nx, colIndexes) |
|||
) |
|||
) |
|||
) |
|||
) |
|||
ENUMFROMTO |
|||
=LAMBDA(a, |
|||
LAMBDA(z, |
|||
SEQUENCE(1 + z - a, 1, a, 1) |
|||
) |
|||
) |
|||
FOLDL |
|||
=LAMBDA(op, |
|||
LAMBDA(a, |
|||
LAMBDA(xs, |
|||
IF( |
|||
2 > ROWS(xs), |
|||
op(a)(xs), |
|||
FOLDL(op)( |
|||
op(a)( |
|||
HEAD(xs) |
|||
) |
|||
)( |
|||
TAIL(xs) |
|||
) |
|||
) |
|||
) |
|||
) |
|||
) |
|||
HEAD |
|||
=LAMBDA(xs, |
|||
INDEX(xs, 1, SEQUENCE(1, COLUMNS(xs))) |
|||
) |
|||
TAIL |
|||
=LAMBDA(xs, |
|||
INDEX( |
|||
xs, |
|||
SEQUENCE(ROWS(xs) - 1, 1, 2, 1), |
|||
SEQUENCE(1, COLUMNS(xs)) |
|||
) |
|||
)</lang> |
|||
{{Out}} |
|||
{| class="wikitable" |
|||
|- |
|||
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx |
|||
! colspan="2" style="text-align:left; font-family:Arial, Helvetica, sans-serif !important;"|=FIBONACCI2(32) |
|||
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;" |
|||
| |
|||
| A |
|||
| B |
|||
|- style="text-align:center;" |
|||
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1 |
|||
| style="text-align:right; font-weight:bold" | |
|||
| Fibonacci |
|||
|- style="text-align:center;" |
|||
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2 |
|||
| style="text-align:right; font-weight:bold" | N=32 |
|||
| style="background-color:#cbcefb;" | 2178309 |
|||
|} |
|} |
||