The Monthly Course: Mathemathics

Hole 1: Cantor

Display the Cantor set. This is one of the simplest fractals, discovered by Georg Cantor. It is the result of an infinite process, so for this hole, printing an approximation of the whole set is enough.

The argument passed to your script will be a single digit between 0 and 8 inclusive indicating the order of the approximation.

The following steps describe one way of obtaining the desired output:

  1. Start with a string of dashes, with length 3**$ARGV[0].

  2. Replace the middle third of the line of dashes with spaces. You are left with two lines of dashes at each end of the original string.

  3. Replace the middle third of each line of dashes with spaces. Repeat until ready.

For example, if the argument is 3, start with a string of 27 dashes:


Remove the middle third of the string:

    ---------         ---------

and remove the middle third of each piece:

    ---   ---         ---   ---

and again:

    - -   - -         - -   - -

The process stops here, when the lines of dashes are all of length 1.

You should not print the intermediate steps. Only the final result, given by the last line above, should be displayed. Your output should be properly newline-terminated.

Hole 2: Kolakoski

Print a Kolakoski sequence. This is a sequence of numbers that describes itself.

The string ``122112122122112...'' consists of alternating substrings of 1's and 2's: ``1'', ``22'', ``11'', ``2'', ``1'', ... . If you write down the lengths of these substrings: 1, 2, 2, 1, 1, ..., you get the numbers in the original string back. A string of numbers with this property is called a Kolakoski sequence.

You can make a Kolakoski sequence of any two numbers from 1 to 9. For example, the following is the Kolakoski sequence of 3 and 4: ``33344433344443333...''. All Kolakoski sequences are uniquely defined by the two alternating numbers, and are infinitely long.

Your script should take exactly three arguments. The first two are the numbers that should be used in the Kolaski sequence. They are two distinct numbers between 1 and 9 inclusive, written as single digits. The third argument will be a number between 1 and 500 inclusive. It is the number of characters that should be printed. Your program must print exactly that number of characters, no more and no less, followed by a newline character.

General rules


The game starts May 1st (00:00 UTC) and ends May 8th (00:00 UTC).

Test program

A test program is provided to help screen entries.

Any program that passes the test program should be submitted. If you are surprised that your solution passed the test program, please submit it anyway! That will help us identify bugs in the test program.

For the test program to work correctly, you will have to name your scripts and and place them in the same directory as your test program. Run the test program:

    $ perl

to verify that your entries are valid. The test script can detect if you have only provided a solution for one of the holes, and in that case will check that hole only.

Passing the test program does not assure your solution is valid. The referees have the final say.


You can submit your solutions here (you'll notice it's the same page as the Leaderboard). You should not submit solutions for both holes at the same time. Only golfers that have solved both holes will appear on the Leaderboard.

Do not publish your solutions anywhere. That will spoil the game, as your solutions are meant to be secret. All solutions will be published at the end of the game.

Prizes (provided by O'Reilly and ActiveState) will be awarded to veteran and beginner winners. A prize may also be awarded to any especially interesting artistic and/or unorthodox solutions.


You can track your ranking through the leaderboard here. Beginners are encouraged to enter and there is a separate leaderboard for them.

New this month is a special board for teams. There will be no prizes awarded to the best team, other than the admiration of your fellow golfers. If you are in a team, you can't also play individually.


We encourage you to send feedback as well as your ideas for future holes and tiebreakers to


If you want to be a referee next month, drop us a note: