
Writing about C++, Programming, FOST.3™, Mahlee™, the web, Thailand and anything else that catches my attention—with some photos thrown in
I'm sure everybody who has ever played around with computer programming has at one point or another written some code that draws the Mandelbrot set. It turns out that there's a cool way to calculate π in there too.
If you've not implemented complex numbers before they're fairly simple. A complex number is a binary tuple where the first number is real and the second imaginary, (x, yi). i is the square root of -1. Squaring these numbers is fairly simple:
let sqc (r, i) = ( r * r - i * i, 2 * r * i )
And adding them is even simpler:
let add (r1, i1) (r2, i2) = (r1 + r2, i1 + i2)
The basics of the sequence are to convert the x and y screen position into a complex number over the range -1 to 1 on each axis and then keep squaring and adding:
let step o v = add o (sqc v)
Where o is the original number and v is the result of the last iteration step. The steps are always started with a point at the origin, so at step 0 the value is 0, and at step 1 the value is the complex number we're calculating for.
The normal way to determine if a given location is outside the Mandelbrot is to keep doing the calculation step until the distance from the origin is more than 2 — once we're more than 2 away from the origin then we know that the calculation will keep producing ever bigger numbers. Inside the set the iteration will never complete as the calculation goes into an infinite loop. The distance from the origin is done in the obvious way using Pythagoras' theorem (but now we're not taking the y as a complex number, it's simply a distance along the axis):
let length (r, i) = sqrt (r * r + i * i)
The calculation involves using complex numbers where the real component is -0.75 and is really simple. You decide how accurately you want to calculate π, say we want ±0.2 (the error term), we then do the steps from the Mandelbrot counting how many steps we need until the distance goes to 2 or more. π is the number of steps times our error term takes to get to a distance of 2 or more.
Doing the calculation we get 15 steps so π is 15 × 0.2 ±0.2. We can reduce the error term to get more accurate results.
So, go away and have a play, get some values of π and see how long it takes to calculate
After a short hiatus due to Christmas and new year — Happy New Year — the puzzles are back
There are 1609 meters to a mile and the Golden Ratio is about 1.618. This is just close enough to be able to use consecutive numbers in the Fibonacci sequence to convert between miles and kilometres. It works because consecutive numbers in the Fibonacci sequence approximate the Golden Ratio.
Write a program that converts from miles to kilometres (and back again) using the Fibonacci sequence where the numbers to convert are in the sequence
I.e., if 5 is input this is the equivalent of 8 kilometres if the 5 is taken as miles and is equivalent to around 3 miles if the 5 is taken as kilometres.
If we have to convert a number that isn't part of the sequence then we will need to find numbers in the sequence that add up to the number we want to convert.
Change the program so that it can convert numbers that are not in the sequence
I.e. for 100 we would want to find numbers in the Fibonacci sequence that add up to 100, convert them and then add up the results. So, 100 = 89 + 8 + 3:
This means that 100 is 55 + 5 + 2 = 62 miles and 144 + 13 + 5 = 162km.
Add in the real conversions using one mile as 1,609.344 metres and display the error alongside the results
100 is actually 62.137… miles so the error is about 0.2%. 100 is also 160.9344 km so the error in the other direction is about 0.7%.
The next version of Fost has just been released and tagged in our repository.
There has been major work on the build system and the internet library as well as a large number of other small and large changes.
We think that this version should be better on OS/X as well. By the next release we should have better access to Macintosh machines so the Mac support should be more stable in future too.
We'll be working more on the documentation and examples and we'll be making it simpler to use the libraries, especially on Ubuntu where the use of the built in Boost packages now means we can package the Fost libraries and development files as well.
We are also planning on finalising the JSON API so that we can declare that part stable and get it documented.
4.10.03.x is due out on March 21st, 2010.
The tagged release is at the following locations:
Sorry for the lack of puzzle last time — I was flying to Pusan (Korea) and didn't get time to put anything together.
Anyway, there's always some interest when somebody posts not only a puzzle, but also offers a cash reward for solving it. This is what is happening over at the Computational Complexity blog where there's $289 for managing to solve a graph colouring problem.
This sort of map colouring problem is quite well known in many computer circles as the original four colour problem was the first major mathematical proof that was conducted with the help of a computer program.
We can represent the four colours by the numbers zero to three and simplify the problem somewhat
If we start off with a 1 x 1 grid then we only need 1 colour. A 2 x 2 grid requires 2 colours (the squares in opposite corners aren't adjacent so can have the same colour). As the we increase the size of the grid then the number of colours we need should go up, but will never exceed four.
Write a program that can colour a n x m grid.
The output of the program should be m lines of n digits that represent the colours that are used. I.e. for the 2 x 2 case we can have:
0 1 1 0
Write a program that can colour a n x m grid cycling through the colours one at a time.
I.e. the program starts by placing colour 0 somewhere, then it places colour 1, then colour 2 etc. When it's placed colour 3 it needs to place 0 again. If n x m is divisible by four then there should be equal numbers of each colour. For 2 x 2 we might have:
0 1 2 3
If we interpret each line as a number and sum the numbers we get for the rows, write a program that colours the squares with the minimum sum.
I.e. the above solution produces a total of 24 (1 + 23), but if we move the digits around a bit we can get:
0 3 1 2
This produces a sum of 15 so is better.
What are the lowest sums you can get for a 5 x 5, a 10 x 10 and a 20 x 20 grid?
Just posting the sums is enough.
Mike has put together a different sort of obfuscatory challenge. You need a fairly modern browser (and possibly a bit of luck as well unless you have Safari) to get it working.