100 Doors
100 doors is a relatively simple mathematical problem. If you have 100 doors that are initially closed, and follow this procedure:
Walk past the doors 100 times, the first time you toggle every door (if the door is closed, you open it, and vice versa). The second time, you only visit every second door, the third time every third door. This continues until you only visit the 100th door.
.. what doors are remaining open?
class Door {
state {_state}
construct new() {
_state = false
}
changeState() {
_state = !_state
}
}
class Doors {
get {_array}
construct new(size) {
_array = []
for (i in 1..size) {
_array.add(Door.new())
}
}
}
var doors = Doors.new(100)
var step = 1
var result = 1
while (step != 100) {
var count = 1
var stepNextPoint = step
for (door in doors.get) {
if (count >= stepNextPoint) {
door.changeState()
stepNextPoint = stepNextPoint + step
}
count = count + 1
}
step = step + 1
}
for (door in doors.get) {
if (door.state) {
System.print("%(result) door is open")
}
result = result + 1
}