The 100 door problem

Retro Computing twitter is a much more tolerant place than covid or politics twitter. By way of an example I had a lovely exchange with fellow retro enthusiast Michael Doornbos yesterday about an article he published on his blog concerning the 100 door problem.

The problem is described as follows:

There are 100 closed doors in a row. You walk past the doors 100 times. The first time, you visit every door. If a door is open you close it, if it is closed you open it. The second time you walk past the doors you visit every 2nd door and change its state (if it was open, close it; if it was closed, open it). The third time you visit every third door … and so on.

The algorithm is simple – but what Michael did was to use the screen memory on various different computers to hold and display the state of each door, rather than an array. A neat memory saving trick, especially on a 3.5 kilobyte VIC-20.

I took up his challenge and reimplemented the algorithm on my Sharp MZ-700 using the standard S-BASIC interpreter. It should also work on the MZ-80K without alteration as I took care not to use colour, the extended character set or any S-BASIC extensions not in SP-5025.

My code is below.

100 door problem - S-BASIC listing
100 door problem – S-Basic listing

This version of the code takes around 4 seconds to run on an unmodified MZ-700. It will be slower on the MZ-80K of course as the Z80 cpu is clocked more slowly.

I quickly realised that there was more fun to be had with this. The MZ series has a stick-person (202) in the standard character set. Why not use that to open and close the doors?!

The modified code is below. The TEMPO and MUSIC statements determine the speed of execution. A little trial and error resulted in a pleasing combination taking 54 seconds to execute.

100 door problem - modified code for the stick-person animation
100 door problem – modified code for the stick-person animation

So all that remains for you to work out before playing the video of the modified version is which doors are open after the program finishes, and what the relationship between the open doors is. Have fun!

The 100 door problem running on the MZ-700.