# Fibonacci numbers (Forth)

### From LiteratePrograms

**Other implementations**: ALGOL 68 | Alice ML | bc | C | C Plus Plus templates | dc | E | Eiffel | Erlang |**Forth**| FORTRAN | Haskell | Hume | Icon | Java | JavaScript | Lisp | Logo | Lua | Mercury | OCaml | occam | Oz | Pascal | PIR | PostScript | Python | Ruby | Scala | Scheme | Sed | sh | sh, iterative | Smalltalk | T-SQL | Visual Basic .NET

The Fibonacci numbers are the integer sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, ..., in which each item is formed by adding the previous two. The sequence can be defined recursively by

- 1 \\ \end{cases} ."/>

Fibonacci number programs that implement this definition directly are often used as introductory examples of recursion. However, many other algorithms for calculating (or making use of) Fibonacci numbers also exist.

## Contents |

## Implementation

The Fibonacci numbers in Forth:

<<fib.fs>>=fib-iter fib-rec main

Note that even these implementations are only suitable for small values of *n*, since the Fibonacci function grows exponentially and even on a 64-bit machine unsigned Forth cells can only hold the first 92 Fibonacci numbers.

## Iteration

This is the easiest way to calculate fibonacci numbers. The values of fib_{i-2} and fib_{i-1} are stored on the stack, to avoid all the recalculations in the recursive implementation.

**( n -- f )** is a *stack comment* documenting the inputs (n) and outputs (fib_{n}) of the function. It is only a comment, but this style is a de facto standard.

**0 1 rot** ( 0 1 n ) sets up the initial terms of the sequence and rotates the loop count to the top of the stack.

**0 ?do ... loop** ( n -- ) takes the count from the stack leaving the two previous Fibonacci terms on the stack for the body of the loop. **?do** does an initial check against the limit compared to **do**.

**over** ( a b -- a b a ) copies the item under the top of the stack to the top. **dup** ( a -- a a ) duplicates the top item on the stack.

**+** ( a b -- a+b ) adds the top two items on the stack, in this case fib_{n-1} and fib_{n-2}, yielding fib_{n}.

**swap** ( a b -- b a ) swaps the top two items on the stack.

**drop** ( a -- ) gets rid of the extra term before returning from the function.

<<fib-iter>>=: fib-iter ( n -- f ) 0 1rot0 ?doover+swaploopdrop;

## Recursive

This is a recursive implementation. This will become slow on big numbers, because the numbers are recalculated for each recursion.

**dup 2 u< if exit then** returns n if it is 0 or 1. u< is an unsigned comparison compared to <.

**1-** ( n -- n-1 ) is the decrement operator.

**recurse** causes the current function to recurse.

<<fib-rec>>=: fib-rec ( n -- f )dup2 u<ifexitthen1-duprecurseswap1-recurse+ ;

## Test driver

This test driver treats the two Fibonacci functions anonymously. ** ' fib-iter** returns the *execution token* or *xt* of the word, and **execute** performs its action. An xt is similar to a C language function pointer.

<<main>>=: test ( xt count -- ) 0doioverexecute.loopdrop;cr.( fib-iter 0..9: ) ' fib-iter 10 testcr.( fib-rec 0..9: ) ' fib-rec 10 test

Download code |