How to resolve the algorithm 100 doors step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 100 doors step by step in the XPL0 programming language

Table of Contents

Problem Statement

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.

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 100 doors step by step in the XPL0 programming language

Source code in the xpl0 programming language

include c:\cxpl\codes;          \intrinsic 'code' declarations
int     Door(100);              \You have 100 doors in a row
define  Open, Closed;
int     D, Pass, Step;

[for D:= 0 to 100-1 do          \that are all initially closed
        Door(D):= Closed;

Step:= 1;                       \The first time through, you visit every door
for Pass:= 1 to 100 do          \You make 100 passes by the doors
        [D:= Step-1;
        repeat  \if the door is closed, you open it; if it is open, you close it
                if Door(D)=Closed then Door(D):= Open else Door(D):= Closed;
                D:= D+Step;
        until   D>=100;
        Step:= Step+1;          \The second time you only visit every 2nd door
        ];                      \The third time, every 3rd door
                                \until you only visit the 100th door
\What state are the doors in after the last pass?
Text(0, "Open: ");              \Which are open?
for D:= 0 to 100-1 do 
        if Door(D)=Open then [IntOut(0, D+1); ChOut(0,^ )];
CrLf(0);

Text(0, "Closed: ");            \Which are closed?
for D:= 0 to 100-1 do 
        if Door(D)=Closed then [IntOut(0, D+1); ChOut(0,^ )];
CrLf(0);

\Optimized: The only doors that remain open are those that are perfect squares
Text(0, "Open: ");
D:= 1;
repeat  IntOut(0, D*D); ChOut(0,^ );
        D:= D+1;
until   D*D>100;
CrLf(0);
]

  

You may also check:How to resolve the algorithm Halt and catch fire step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Ol programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Generic swap step by step in the DCL programming language