Archive

Posts Tagged ‘prison’

Prison break

September 29, 2010 Leave a comment

Exercise

The big boss of Montreal Prison celebrates his 50th birthday next week. For this special occasion he came up with the idea to let some prisoners go. In the prison there are 600 cells, one person in each. The cells are numbered from 1 to 600. The prison guard should go through all cells and open the doors. Then he goes to the 2nd cell and flips the lock at every second cell (that is, close the door if it was open and open it if it was closed). Then go the 3rd cell and flip the lock of every third cell, etc. This procedure should be repeated with every cell. Question: who are those lucky guys who get release, i.e. which cells remain open at the end?

Example with 8 cells:

00000000        initialization, all cells are closed
11111111        step 1, flip every lock
10101010        step 2, flip every 2nd lock
10001110        step 3, flip every 3rd lock
10011111        step 4, flip every 4th lock
10010111        step 5, flip every 5th lock
10010011        step 6, flip every 6th lock
10010001        step 7, flip every 7th lock
10010000        step 8, flip every 8th lock

Read more…

Categories: python Tags: , , ,