using System; class Fibonacci4 { /** Print ascending members of the Fibonacci sequence that are representable as 128-bit decimal values, 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 71 terms: the fourth item on each line is the difference from the golden ratio). */ static decimal golden_ratio = (1.0M + DecSqrt(5.0M))/2.0M; private static decimal DecSqrt(decimal x) { decimal y; y = (decimal)Math.Sqrt((double)x); // Two Newton-Raphson iterations suffice for decimal precision y = (y + x / y) / 2.0M; y = (y + x / y) / 2.0M; return (y); } public static void Main() { decimal lo = 1M; decimal hi = 1M; decimal n = 1M; decimal ratio; Console.Write("{0,3:N0}", n); Console.Write("\t"); Console.WriteLine("{0,39:N0}", lo); try { while (hi > 0M) { n++; Console.Write("{0,3:N0}", n); Console.Write("\t"); Console.Write("{0,39:N0}", hi); Console.Write("\t"); ratio = hi/lo; Console.Write("{0,37:F30}", ratio); Console.Write("\t"); Console.WriteLine("{0,37:F30}", ratio - golden_ratio); hi = lo + hi; lo = hi - lo; } } catch(System.OverflowException) { } } }