There are 100 doors in a row that are all initially closed.

You make 100 passes by the doors.

The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it).

The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it.

The third time, visit every 3rd door   (door #3, #6, #9, ...), etc,   until you only visit the 100th door.



Task

Answer the question:   what state are the doors in after the last pass?   Which are open, which are closed?


Alternate:
As noted in this page's   discussion page,   the only doors that remain open are those whose numbers are perfect squares.

Opening only those doors is an   optimization   that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.




 
data Door:
  | open
  | closed
end
 
fun flip-door(d :: Door) -> Door:
  cases(Door) d:
    | open => closed
    | closed => open
  end
end
 
 
fun flip-doors(doors :: List<Door>) -> List<Door>:
  doc:```Given a list of door positions, repeatedly switch the positions of
      every nth door for every nth pass, and return the final list of door
      positions```
  for fold(flipped-doors from doors, n from range(1, doors.length() + 1)):
    for map_n(m from 1, d from flipped-doors):
      if num-modulo(m, n) == 0:
        flip-door(d)
      else:
        d
      end
    end
  end
where:
    flip-doors([list: closed, closed, closed]) is
  [list: open, closed, closed]
 
  flip-doors([list: closed, closed, closed, closed]) is
  [list: open, closed, closed, open]
 
  flip-doors([list: closed, closed, closed, closed, closed, closed]) is
  [list: open, closed, closed, open, closed, closed]
 
  closed-100 = for map(_ from range(1, 101)): closed end
  answer-100 = for map(n from range(1, 101)):
    if num-is-integer(num-sqrt(n)): open
    else: closed
    end
  end
 
  flip-doors(closed-100) is answer-100
end
 
fun find-indices<A>(pred :: (A -> Boolean), xs :: List<A>) -> List<Number>:
    doc:```Given a list and a predicate function, produce a list of index
      positions where there's a match on the predicate```
  ps = map_n(lam(n,e): if pred(e): n else: -1 end end, 1, xs)
  ps.filter(lam(x): x >= 0 end)
where:
  find-indices((lam(i): i == true end), [list: true,false,true]) is [list:1,3]
end
 
 
fun run(n):
  doc:```Given a list of doors that are closed, make repeated passes 
      over the list, switching the positions of every nth door for 
      each nth pass. Return a list of positions in the list where the
      door is Open.```
  doors = repeat(n, closed)
  ys = flip-doors(doors)
  find-indices((lam(y): y == open end), ys)
where:
  run(4) is [list: 1,4]
end
 
run(100)
 
 