## The Golden Ratio

### July 10, 2009

I was helping my daughter with her math homework recently, and came across this problem in continued fractions:

This fraction can be considered as a sequence of terms

Or, in general

The first ten elements of the sequence are 1, 2, 3/2, 5/3, 8/5, 13/8, 21/13, 34/21, 55/34, and 89/55.

Your task is to write a program that evaluates the n^{th} element of the sequence. What is the value of ? When you are finished, you may read or run a suggested solution, or post your own solution or discuss the exercise in the comments below.

Pages: 1 2

[...] 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)timeIt’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.

– Golden ratio as a recursive fraction

phiRatio :: Int -> Double

phiRatio n

| n > 0 = 1 + ( 1 / phiRatio (n – 1) )

| otherwise = 1

– Golden ratio as a recursive square root

phiRoot n

| n > 0 = sqrt ( 1 + phiRoot (n – 1) )

| otherwise = 1

I also did one that uses the Fibonacci sequence:

fib :: (Num a, Num b) => a -> b

fib n = fibGen 0 1 n

fibGen :: (Num a, Num b) => b -> b -> a -> b

fibGen a b n = case n of

0 -> a

n -> fibGen b (a + b) (n – 1)

fib :: (Num a, Num b) => a -> b

fib n = fibGen 0 1 n

fibGen :: (Num a, Num b) => b -> b -> a -> b

fibGen a b n = case n of

0 -> a

n -> fibGen b (a + b) (n – 1)

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))))