Streams and golden ratio

Hello,

experimenting with streams I found, by chance, a very simple way to compute the golden ratio. I don't know if it's well known:

(define (improve n) (/ (+ n 1) n))

(define golden-ratio (stream-cons 1 (stream-map improve golden-ratio)))

(displayln (stream->list (stream-take golden-ratio 7)))
; => (1 2 3/2 5/3 8/5 13/8 21/13) here you can see the ratios
;                                 of fibonacci numbers
(displayln (exact->inexact (stream-ref golden-ratio 40)))
; => 1.618033988749895  in less than 40 iterations it's done
;                       maximum precision reached

The improve procedure can seem intuitive, but it's interesting to see how it works:
golden ratio
It starts with 3/2 and ends with 5/3.

Matteo

1 Like

Awesome! Cursorily, this seems "congruent" to the continued fraction for the golden ration, i.e. 1 + 1 / (1 + 1 / (1 + ...)). Good stuff.

2 Likes

The first answer here uses the rule x to 1+1/x which is the same as the improve function.

4 Likes