# Streams and golden ratio

**URL:** <https://racket.discourse.group/t/streams-and-golden-ratio/3152>\
**Category:** Show & Tell\
**Created:** [September 5, 2024, 12:11pm UTC](https://racket.discourse.group/t/streams-and-golden-ratio/3152 "2024-09-05T12:11:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteo-daddio](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/matteo-daddio/32/1245_2.png) [@matteo-daddio](https://racket.discourse.group/u/matteo-daddio)\
**Post date:** [September 5, 2024, 12:11pm UTC](https://racket.discourse.group/t/streams-and-golden-ratio/3152/1 "2024-09-05T12:11:39Z")

</div>

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:

```scheme
(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](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/c/cba0a7eddbd5f767f77ac1e68042dcd9c4d6365f.jpeg)  
It starts with 3/2 and ends with 5/3.

Matteo

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [September 5, 2024, 12:32pm UTC](https://racket.discourse.group/t/streams-and-golden-ratio/3152/2 "2024-09-05T12:32:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [September 5, 2024, 12:45pm UTC](https://racket.discourse.group/t/streams-and-golden-ratio/3152/3 "2024-09-05T12:45:06Z")

</div>

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

> <https://math.stackexchange.com/questions/985996/continued-fraction-of-the-golden-ratio>
