wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> hard >> Circular Jail Cell
(Message started by: NickH on Jul 28th, 2002, 6:50am)

Title: Circular Jail Cell
Post by NickH on Jul 28th, 2002, 6:50am
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.

Title: Re: Circular Jail Cell
Post by horseisahorse on Aug 7th, 2002, 6:00pm
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.


Title: Re: Circular Jail Cell
Post by Ivan on Dec 18th, 2002, 10:20pm
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




Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board