### -*-bc-*- ### ==================================================================== ### Print ascending members of the Fibonacci sequence that are ### representable as 64-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). ### [19-Apr-2002] ### ==================================================================== ### bc supports arbitrary precision floating-point arithmetic. For ### comparison with other programming languages, we limit it to ### 64-bit arithmetic: log10(2^64) = 19.2659..., so we set scale = 20 scale = 20 golden_ratio = (1 + sqrt(5))/2 limit = 2^63 - 1 lo = hi = n = 1 print n, "\t", lo, "\n" while (hi < limit) { n = n + 1 ratio = hi/lo scale = 0 print n, "\t", hi, "\t" scale = 20 print ratio, "\t", (ratio - golden_ratio), "\n" hi = lo + hi lo = hi - lo }