Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.    adapted to Oberon-07 by 0CodErr, KolibriOS team
  3.                                                    *)
  4. (*
  5.    There are 100 doors in a row that are all initially closed.
  6.    You make 100 passes by the doors.
  7.    The first time through, visit every door and toggle the door (if the door is closed, open it; if it is open, close it).
  8.    The second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it.
  9.    The third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
  10.    What state are the doors in after the last pass? Which are open, which are closed?
  11. *)
  12. MODULE Doors;
  13.  
  14. IMPORT In, Out, Console;
  15.  
  16.  
  17. CONST
  18.     CLOSED = FALSE;
  19.     OPEN   = TRUE;
  20.  
  21.  
  22. TYPE
  23.     List = ARRAY 101 OF BOOLEAN;
  24.  
  25.  
  26. VAR
  27.     Doors: List;
  28.     I, J:  INTEGER;
  29.  
  30.  
  31. BEGIN
  32.     Console.open;
  33.  
  34.     FOR I := 1 TO 100 DO
  35.         FOR J := 1 TO 100 DO
  36.             IF J MOD I = 0 THEN
  37.                 IF Doors[J] = CLOSED THEN
  38.                     Doors[J] := OPEN
  39.                 ELSE
  40.                     Doors[J] := CLOSED
  41.                 END
  42.             END
  43.         END
  44.     END;
  45.     FOR I := 1 TO 100 DO
  46.         Out.Int(I, 3);
  47.         Out.String(" is ");
  48.         IF Doors[I] = CLOSED THEN
  49.             Out.String("Closed.")
  50.         ELSE
  51.             Out.String("Open.")
  52.         END;
  53.         Out.Ln
  54.     END;
  55.     In.Ln;
  56.  
  57.     Console.exit(TRUE)
  58. END Doors.