The Golden Ratio
July 10, 2009
A useful function is iterate
, which repeatedly evaluates a function in a sequence:
(define (iterate n f . bs)
(let loop ((n n) (b (car bs)) (bs (cdr bs)) (xs '()))
(if (zero? n) (reverse xs)
(let ((new-bs (append bs (list (apply f b bs)))))
(loop (- n 1) (car new-bs) (cdr new-bs) (cons b xs))))))
For instance, (define (fib n) (iterate n + 1 1))
defines a function that calculates the first n fibonacci numbers. Iterate
is useful enough that we will add it to the Standard Prelude.
The sequence that computes the golden ratio is
(define (golden n) (iterate n (lambda (x) (+ 1 (/ x))) 1))
and the 201st element (note that iterate
counts from zero) of the golden sequence is
> (car (reverse (golden 201)))
734544867157818093234908902110449296423351/453973694165307953197296969697410619233826
This continued fraction is well-known as the value of φ, the golden ratio, an irrational number with a value of or approximately 1.618; by the 200th term, the error is -7.851 × 10-84. The golden ratio is common in nature, art and music.
You can see this program at http://programmingpraxis.codepad.org/tJaZAyBY. The mathematical equations on this page were produced by Donald Knuth’s LaTeX program; for a math pastebin, see MathBin.net.
[…] Praxis – The Golden Ratio By Remco Niemeijer Today’s Programming Praxis problem is an easy one: all we have to do is make a function that calculates the […]
My Haskell solution (see http://bonsaicode.wordpress.com/2009/07/10/programming-praxis-the-golden-ratio/ for a version with comments):
Regular continued fractions have the nice property that they can be efficiently computed in the forward relation by a well-known recurrence:
In this case,
phi = cf (fun _ -> 1)
Ruby:
Python’s native bigint support makes this easy.
Couple of quick Clojure solutions, first a simple recursive solution:
(defn g [n]
(if (zero? n) 1
(inc (/ 1 (g (dec n))))))
And a more space efficient iterative solution:
(defn g [n]
(loop [x 1 n n]
(if (zero? n) x
(recur (inc (/ 1 x)) (dec n)))))
Ooops, sorry I hadn’t read the “HOWTO” on posting source:
[ EDIT: Clojure is not one of the supported languages. I changed the sourcecode tag to css. ]
Recursive:
Iterative:
Since this was posted for my math class (I’m the daughter mentioned in the problem), I was just wondering if anyone here can solve it without using computer programming. The reason I ask is that some interesting mathematical constants start to appear as you solve using algebra. If you don’t remember high school algebra, then solving the first ten numbers by had and leaving the fractions in improper form will reveal some sequences as well. That’s what the homework was actually designed to teach us. Just a thought.
[…] […]
A little ruby solution…
fib = Hash.new {|h,x| x == 0 ? h[x] = 0 : x == 1 ? h[x] = 1 : h[x] = h[x-1] + h[x-2]}
g = lambda {|x| [fib[x+2],fib[x+1]]}
puts g[200].join(‘ / ‘)
Anne,
As you noticed, the numerators and denominators of the terms are Fibonacci numbers. That’s because there is an intimate relationship between the Fibonacci numbers and the Golden Ratio. In particular, the ratio of n+1st and nth Fibonacci numbers tend to the Golden Ratio as n gets large. The same thing happens for the continued fraction as your dad illustrated. See http://www.friesian.com/golden.htm for an explanation of this that should be accessible to anyone with a little algebra background.
Pretty straightforward in Python. Simultaneous assignment makes it look pretty.
Before I really thought about the problem very much I tried coding it up in C#, which was easy enough. It surprised me when it overflowed a 64-bit unsigned integer. The reason why it caused an overflow became obvious once I took a look at the comments here. The code is just calculating a Fibonacci number. G(n) = F(n+2)/F(n+1). A true solution would require a bigint implementation. Without resorting to that, I rewrote the code using doubles. I thought it was interesting to discover that after G(40), the error in the approximation of the golden ratio became too small to store in a double.
http://inscrutable.pastebin.com/f2dc90711
PLT Scheme:
(define/contract (continued-fraction-term N)
(-> (and/c (not/c negative?)
integer?)
number?)
(if (zero? N)
1.
(add1 (/ (continued-fraction-term (sub1 N))))))
Trying to get hold of prolog :)
A solution in O(log n) time
It’s nice to see this thread still active. Here’s a solution that runs
in log n time. It’s based on Anne’s observation that Gn = Fibn+1
/ Fibn (easily shown by a simple induction) and the fast-fib routine
from Exercise 1.19 of SICP.
I also did one that uses the Fibonacci sequence:
As for Clojure, there’s an even shorter solution than Darren’s:
We could have also BigDecimals, for more precision, like so:
I’m working my way through old exercises in an effort to learn Common Lisp.
(defun fibonacci (n &optional (a 0) (b 1))
“Accepts a number and fetches the corresponding number in the Fibonacci sequence”
(if (< n 2)
(* n b)
(fibonacci (- n 1) b (+ a b))))
(defun goldenr (n)
"Accepts a number and approximates the golden ratio to that step of certainty"
(/ (fibonacci n) (fibonacci (- n 1))))
Haskell, using the definitions supplied in the top portion (G0 = 1, G1 = 2 … )
golden :: Int -> Rational
golden 0 = 1
golden 1 = 2
golden n = 1 + (1 / (golden (n-1)))
OUTPUT:
———–
ghci> golden 200
734544867157818093234908902110449296423351 % 453973694165307953197296969697410619233826