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

Published on 12 May 2024 09:40 PM
#Dc

How to resolve the algorithm 100 doors step by step in the Dc 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 Dc programming language

Source code in the dc programming language

## NB: This code uses the dc command "r" via register "r".
##     You may comment out the unwanted version.
[SxSyLxLy]sr    # this should work with every "dc"
[r]sr           # GNU dc can exchange top 2 stack values by "r"
## Now use "lrx" instead of "r" ...

0k              # we work without decimal places
[q]sq           # useful e.g. as loop termination

## (x)(y)>R  ==  if (y)>(x) eval R
## isle         x y --> (x <= y)
[
    [1q]S. [ !<. 0 ]x s.L.
]sl
## l: isle

[
    100 llx
]sL
## L: isle100

## for  initcode condcode incrcode body
##       [1]      [2]      [3]      [4]
[
    [q]S. 4:. 3:. 2:. 1:.  1;.x [2;.x 0=. 4;.x 3;.x 0;.x]d0:.x Os.L.o
]sf
## f: for
##----------------------------------------------------------------------------

##      for( i=1 ; i<=100 ; ++i ) {
##          door[i] = 0;
##      }
#[init ...]P []ps-
[1si] [li lLx] [li1+si] [
    li 0:d
]lfx

##      for( s=1 ; s<=100 ; ++s ) {
##          for( i=s ; i<=100 ; i+=s ) {
##              door[i] = 1 - door[i]
##          }
##      }
[1ss] [ls lLx] [ls1+ss] [
    #[step ]P lsn [ ...]ps-
    [lssi] [li lLx] [lils+si] [
        1 li;d - li:d
    ]lfx
]lfx

##      long output:
##          for( i=1 ; i<=100 ; ++i ) {
##              print "door #", i, " is ", (door[i] ? "open" : "closed")), NL
##          }
[
    [1si] [li lLx] [li1+si] [
        [door #]P
        li n
        [ is ]P
            [closed]
            [open]
        li;d 0=r lrx s- n
        [.]ps-
    ]lfx
]

##      terse output:
##          for( i=1 ; i<=100 ; ++i ) {
##              if( door[i] ) {
##                  print i
##              }
##              print NL
##          }
[
    [1si] [li lLx] [li1+si] [
        [] [ [ ]n lin ]
        li;d 0=r lrx s- x
    ]lfx
    []ps-
]

lrx             # comment out for the long output version
s- x
#[stack rest...]P []ps- f

  

You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Retro programming language
You may also check:How to resolve the algorithm Commatizing numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Active Directory/Connect step by step in the Erlang programming language
You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the PL/I programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the Go programming language