Fibonacci series

Programming in Forth
Post Reply
Admin
Site Admin
Posts: 15
Joined: Tue Apr 02, 2019 5:29 pm

Fibonacci series

Post by Admin » Tue May 14, 2019 1:35 pm

The wordS below will produce the Fibonacci sequence of numbers. This starts at 0 1 and the next number is the sum of the previous two. The maximum number in the sequence is limited by the maximum positive integer used in H4th.

We can only go to 24 numbers using 16 bit integers.
: FIB 0 DUP . 1 DUP .
22 0 DO
DUP ROT + DUP .
LOOP ;


Using 32 bit integers, we can go to 48 numbers

VARIABLE F ALLOT 6
: FIB 0 . 0 S->D 1 S->D
F D! F 4 + D@ D!
46 0 DO
F D@ F 4 + D@ D+
F 4 + D@ F D! F 4 + D!
F 4 + D@ D.
LOOP ;

Post Reply