MODULE Fibonacci3o;
(***********************************************************************
Print ascending members of the Fibonacci sequence that are representable
as 32-bit signed integers, prefixed by their term numbers, and followed
by the ratio of successive terms, to demonstrate the 1.618...^n growth
(the ratio approaches the golden ratio, (1 + sqrt(5))/2 =
1.6180339887498949, and reaches it (to machine precision) at 41 terms:
the fourth item on each line is the difference from the golden ratio).
***********************************************************************)
IMPORT Ascii,
LRealMath,
StdChannels,
TextRider;
VAR
lo : LONGINT;
hi : LONGINT;
n : LONGINT;
diff : LONGREAL;
golden_ratio : LONGREAL;
ratio : LONGREAL;
writer : TextRider.Writer;
BEGIN
writer := TextRider.ConnectWriter(StdChannels.stdout);
golden_ratio := (1.0D0 + LRealMath.sqrt(5.0D0))/2.0D0;
lo := 1;
hi := 1;
n := 1;
writer.WriteLInt(n, 2);
writer.WriteChar (Ascii.ht);
writer.WriteLInt(lo, 12);
writer.WriteLn;
WHILE (hi > 0) DO
n := n + 1;
ratio := hi;
ratio := ratio / lo;
writer.WriteLInt(n, 2);
writer.WriteChar (Ascii.ht);
writer.WriteLInt(hi, 12);
writer.WriteChar (Ascii.ht);
writer.WriteLRealFix(ratio, 25, 15);
writer.WriteChar (Ascii.ht);
diff := ratio - golden_ratio;
writer.WriteLRealFix(diff, 25, 15);
writer.WriteLn;
hi := lo + hi;
lo := hi - lo
END
END Fibonacci3o.