Doors.ob07 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. IMPORT In, Out, Console;
  14. CONST
  15. CLOSED = FALSE;
  16. OPEN = TRUE;
  17. TYPE
  18. List = ARRAY 101 OF BOOLEAN;
  19. VAR
  20. Doors: List;
  21. I, J: INTEGER;
  22. BEGIN
  23. Console.open;
  24. FOR I := 1 TO 100 DO
  25. FOR J := 1 TO 100 DO
  26. IF J MOD I = 0 THEN
  27. IF Doors[J] = CLOSED THEN
  28. Doors[J] := OPEN
  29. ELSE
  30. Doors[J] := CLOSED
  31. END
  32. END
  33. END
  34. END;
  35. FOR I := 1 TO 100 DO
  36. Out.Int(I, 3);
  37. Out.String(" is ");
  38. IF Doors[I] = CLOSED THEN
  39. Out.String("Closed.")
  40. ELSE
  41. Out.String("Open.")
  42. END;
  43. Out.Ln
  44. END;
  45. In.Ln;
  46. Console.exit(TRUE)
  47. END Doors.