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.




 
DEFINE PROCEDURE ''DIVIDE'' [A,B]:
BLOCK 0: BEGIN
  IF A < B, THEN:
    QUIT BLOCK 0;
  CELL(0) <= 1;
  OUTPUT <= 1;
  LOOP AT MOST A TIMES:
  BLOCK 2: BEGIN
    IF OUTPUT * B = A, THEN:
    QUIT BLOCK 0;
    OUTPUT <= OUTPUT + 1;
    IF OUTPUT * B > A, THEN:
    BLOCK 3: BEGIN
      OUTPUT <= CELL(0);
      QUIT BLOCK 0;
    BLOCK 3: END;
    CELL(0) <= OUTPUT;
  BLOCK 2: END;
BLOCK 0: END.
 
DEFINE PROCEDURE ''MINUS'' [A,B]:
BLOCK 0: BEGIN
  IF A < B, THEN:
    QUIT BLOCK 0;
  LOOP AT MOST A TIMES:
  BLOCK 1: BEGIN
    IF OUTPUT + B = A, THEN:
      QUIT BLOCK 0;
    OUTPUT <= OUTPUT + 1;
  BLOCK 1: END;
BLOCK 0: END.
 
DEFINE PROCEDURE ''MODULUS'' [A,B]:
BLOCK 0: BEGIN
  CELL(0) <= DIVIDE[A,B];
  OUTPUT <= MINUS[A,CELL(0) * B];
BLOCK 0: END.
 
 
 
DEFINE PROCEDURE ''TOGGLE'' [DOOR]:
BLOCK 0: BEGIN
  IF DOOR = 1, THEN:
    QUIT BLOCK 0;
  OUTPUT <= 1;
BLOCK 0: END.
 
DEFINE PROCEDURE ''NUMBERS'' [DOOR, COUNT]:
BLOCK 0: BEGIN
  CELL(0) <= 1; /*each number*/
  OUTPUT <= 0; /*current door state*/
 
  LOOP COUNT TIMES:
  BLOCK 1: BEGIN
 
    IF MODULUS[DOOR, CELL(0)] = 0, THEN:
      OUTPUT <= TOGGLE[OUTPUT];
 
    CELL(0) <= CELL(0) + 1;
 
  BLOCK 1: END;
 
BLOCK 0: END.
 
DEFINE PROCEDURE ''DOORS'' [COUNT]:
BLOCK 0: BEGIN
 
  CELL(0) <= 1; /*each door*/
  LOOP COUNT TIMES:
  BLOCK 1: BEGIN
 
    CELL(1) <= NUMBERS[CELL(0), COUNT];  /*iterate over the states of this door to get its final state*/
    IF CELL(1) = 1, THEN: /*door state = open*/
      PRINT[CELL(0), '   '];
 
    CELL(0) <= CELL(0) + 1;
 
  BLOCK 1: END;
BLOCK 0: END.
 
DOORS[100];
 