MODULE Fibonacci4o; (*********************************************************************** 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, Integers, LRealMath, Reals, StdChannels, TextRider; VAR s : ARRAY 1024 OF CHAR; t : ARRAY 1024 OF CHAR; lo : Integers.Integer; hi : Integers.Integer; limit : Integers.Integer; n : LONGINT; diff : Reals.Real; golden_ratio : Reals.Real; ratio : Reals.Real; writer : TextRider.Writer; BEGIN writer := TextRider.ConnectWriter(StdChannels.stdout); golden_ratio := Reals.div(Reals.add(Reals.Long(1.0D0), Reals.sqrt(Reals.Long(5.0D0))), Reals.Long(2.0D0)); lo := Integers.Long(1); hi := Integers.Long(1); limit := Integers.Entier(LRealMath.power(2.0D0,63.0D0) - 1.0D0); n := 1; writer.WriteLInt(n, 2); writer.WriteChar (Ascii.ht); Integers.ConvertToString(lo,s); writer.WriteString(s (*, 12 *)); writer.WriteLn; WHILE (Integers.Compare(hi,limit) < 0) DO n := n + 1; Integers.ConvertToString(hi,s); Integers.ConvertToString(lo,t); ratio := Reals.div(Reals.ToReal(s), Reals.ToReal(t)); writer.WriteLInt(n, 2); writer.WriteChar (Ascii.ht); Integers.ConvertToString(hi,s); writer.WriteString(s); writer.WriteChar (Ascii.ht); Reals.ToString(ratio, s, 40); writer.WriteString(s); writer.WriteChar (Ascii.ht); diff := Reals.sub(ratio,golden_ratio); Reals.ToString(diff, s, 40); writer.WriteString(s); writer.WriteLn; hi := Integers.Sum(lo,hi); lo := Integers.Difference(hi,lo) END END Fibonacci4o.