Skip to content

Instantly share code, notes, and snippets.

@llliiu
Last active January 27, 2017 01:26
Show Gist options
  • Save llliiu/6e95b1d0f4776c2171d0d18baba21984 to your computer and use it in GitHub Desktop.
Save llliiu/6e95b1d0f4776c2171d0d18baba21984 to your computer and use it in GitHub Desktop.
100 doors problem
# MAT 594E: Workshop2, "100 doors"
# Problem: you have 100 doors in a row that are all initially closed. You make 100 passes by the doors starting with the first door every time. the first time through you visit every door and toggle the door (if the door is closed, you open it, if it is open, you close it). the second time you only visit every 2nd door (door #2, #4, #6). the third time, every 3rd door (door #3, #6, #9), etc, until you only visit the 100th door.
# Question: what state are the doors in after the last pass? which are open which are closed?
# Author: Lu Liu
# Date: 01/26/2017
# An array for status of doors // set 0 as closed door, 1 as opened door
lock = [False]]*100
# An array for 100 doors index
doors = range(1,101)
numOpen = 0
# A function to check how many doors are still open
def NumOpen():
numOpen = 0
for a in range(0,100):
if lock[a] == True:
numOpen += 1
return numOpen
loopTimes=1
while True:
loopTimes=1
doors= range(1,101,loopTimes)
for j in range(1,loopTimes):
for i in range(len(doors)):
if lock[i] == False:
lock[i] == True
print lock[i]
else:
lock[i] == False
print lock[i]
NumOpen()
if numOpen =1:
break
else:
loopTimes += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment