Author |
Topic: Circular Jail Cell (Read 3383 times) |
|
NickH
Senior Riddler
Gender:
Posts: 341
|
|
Circular Jail Cell
« on: Jul 28th, 2002, 6:50am » |
Quote Modify
|
Ten prisoners found their door open after 100 rounds of the drunken jailor. They were the ones whose cell number is a perfect square. Each door is opened or closed once for each factor of its cell number, including 1 and the cell number itself. For example, for cell 12: 1, 2, 3, 4, 6, 12. Most whole numbers have an even number of factors. This is because each factor f can be paired with n/f. The exceptions are the perfect squares, where f = n/f for precisely one factor.
|
|
IP Logged |
Nick's Mathematical Puzzles
|
|
|
horseisahorse
Guest
|
Actually, I think the real-world answer would be zero, since each prisoner would have booked out of the prison as soon as the jailor opened his door during the first round.
|
|
IP Logged |
|
|
|
Ivan
Guest
|
I got the same answer as NickH. Since I like doing things the hard way, I wrote a python program (the formatting here ain't great, look at it at http://linuxhelp.hn.org/jail-cell.py if you're interested. (with proper block spacing)): Code: #!/usr/bin/python doors = {} # create doors 1 - 100 for x in xrange(101): doors[x] = 0 del doors[0] # first, the jailer goes to each cell # this number will be increased in each round sep = 1 # repeat until jailer falls down while 1: doorcount = 1 for x in doors: # check if the door by the current door skipping factor if doorcount % sep: if doors[x] == 0: # if it's closed, open it doors[x] = 1 else: # if it's open, close it doors[x] = 0 doorcount += 1 sep += 1 if sep > 100: # jailer falls down break # count how many doors are open in the end doors_open = 0 for x in doors: if doors[x] == 1: doors_open += 1 print doors_open |
|
|
|
IP Logged |
|
|
|
|