At the first pass the status of all doors is changed (i.e. all doors are open now because they were all closed before).
At the second pass only every second door toggles (door 2, 4, 6 ...)
At the third pass every third door toggles (door 3, 6, 9 ...)
At the fourth pass every fourth door toggles (door 4, 8, 12 ...) etc.
At the last (100th) pass only the status of the 100th door is changed.
What state are the doors after the last pass? Which are open, which are closed?
more infos: http://rosettacode.org/wiki/100_doors
The program contains two different approaches/algorithms:
LBL A uses 100 register for the door-status of each door and opens/closes them in 100 passes.
LBL B simulate the 100 passes for each door and only needs 3 register.
Usage: XEQ "DOOR"
Display: A:PRN B:VIEW
<A> prints the status of all 100 doors on the printer (needs SIZE>=103).
<#> <B> shows the status of the doors starting with # one after an other.
Output (e.g.):
D16: 5 O (door 16 toggels 5 times open)
D23: 2 C (door 23 toggels 2 times closed)
Code: Select all
LBL "DOOR"
FIX 0
CF 29
SF 27
1 ; default # for <B>
"A:PRN B:VIEW"
PROMPT
LBL A
CLA
CLRG
2
STO 02
2
STO 01
LBL 01
1
ST+ 01
RCL 01
103
X=Y?
GTO 02
LBL 06
RCL 01
2
-
ST+ 02
RCL 02
103
X<=Y?
GTO 03
1
ST+ IND 02
GTO 06
LBL 03
2
STO 02
GTO 01
LBL 02
CLX
STO 01
LBL 04
3
STO 01
LBL 10
RCL 01
2
-
"|- D" ; door
ARCL X
"|-: "
RCL IND 01
ARCL X
2
MOD
X#0?
"|- O" ; open
X=0?
"|- C" ; close
RCL 01
2
MOD ; print two results in one line
X=0?
PRA ; or AVIEW to test without printer
X=0?
CLA
1
ST+ 01
103
RCL 01
X#Y?
GTO 10
RTN
LBL B
LBL 00
CLRG
STO 01
2
/
INT
STO 02
LBL 05
RCL 01
RCL 02
MOD
X=0?
ISG 03
X<>Y
DSE 02
GTO 05
RCL 03
1
+
"D" ; door
ARCL 01
"|-: "
ARCL X
2
MOD
X=0?
"|- C" ; close
X#0?
"|- O" ; open
AVIEW
PSE
1
RCL 01
+
101
X<>Y
X#Y?
GTO 00
END