The Golden Ratio

Also known as the “Divine Quotient”, the Golden Ratio was given an almost magical meaning during the renaissance, but it’s actually much older than that. Leonardo DaVinci used it. Euclid used it. It was supposedly discovered by Pythagoras. So, what is it? It’s very simple. Take a length and divide it into two parts: a and b:

When the ratio between a and b is equal to the ratio between a+b and a, then you have the Golden Ratio. The value of the Golden Ratio is:

Why is it so special? Because if you know about it, it can be seen in many different places; in geometrical figures, in architecture, in paintings, and in nature. I’ll show you. Draw a pentagon:

Pentagon

Draw straight lines between all vertices of the pentagon. This will result in a pentagram (five-pointed star). Now mark the lengths a and b as this picture shows:

Pentagram inside pentagon

Guess what the ratio is between the lengths a and b? The Golden Ratio. Now, mark two other lengths a' and b':

Pentagram inside pentagon with other lengths

You guessed it. The ratio between a' and b' is the Golden Ratio.

It’s actually pretty easy to approximate Phi (φ), as the Golden Ratio normally is called. Take any two numbers and add them, then divide the sum with the larger number. For example, 11 and 74:

Now we add the larger of the numbers (74) to the sum (85), and then we again divide the new sum (159) with the the previous sum (85):

We’re getting there. One more time:

No, this is too tedious. Let’s write a program that does it. For no reason whatsoever, I’ll pick Scala. What is Scala? I’m on a Mac, so I’ll just do:

This is how I install Scala:

I’ll want some editor support, so I install the TextMate bundle that accompanies Scala:

I’ll tell TextMate to reload its bundles by selecting the menu “Bundles | Bundle Editor | Reload Bundles”. Now I’m all set.

Consider this Scala program (phi1.scala):

I’ll run it like this:

It’s not pretty, but it does the job. Let’s spice it up with some input arguments and better printout (phi2.scala):

I’ll run it with four arguments to test it:

I won’t bore you with more tests, but instead do what we intended to do from the start: have a program run the iterations for us. I’ll run it with no arguments, using the defaults:

Actually, I will run one more test, but this time I’ll choose the starting values more carefully. One of many wonderful properties of the Fibonacci series is that the ratio between the numbers is very close to the Golden Ratio. This means it will converge much quicker. Check this out, four correct decimals after just four iterations:

Here is the final version, with some stuff that makes the file executable (phi.scala):

I’ll make it executable and run it:

Check out Golden Ratio on Wikipedia for more details.

This Post Has One Comment

  1. I had expected some Scala wizards to criticize my feeble attempt at coding an algorithm in Scala, but it didn’t happen. So I’ll do it myself instead.

    What do I really want to do? If n is 1, the larger number should be divided by the smaller number. Otherwise, calculate the sum of the numbers and repeat on the larger number and the sum, for one less iteration.

    I’ll start the interactive Scala shell and type this in:

    scala> def phi(a:Double, b:Double, n:Int): Double = n match {
    | case 1 => (a max b)/(a min b)
    | case _ => phi((a max b), a+b, n-1)
    | }
    phi: (Double,Double,Int)Double

    Testing it, and what do you know? It works:

    scala> phi(5., 8., 20)
    res1: Double = 1.618033988957902

Leave a Reply

Close Menu