100 doors: Difference between revisions

Content deleted Content added
Ssmitch45 (talk | contribs)
Heiner (talk | contribs)
Dc: first implementation
Line 3,325: Line 3,325:
}
}
</lang>
</lang>

=={{header|Dc}}==
'''Unoptimized''':
{{works with|dc|(GNU bc 1.06.95) 1.3.95}}
<lang Dc>
## 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
</lang>
Output:
1 4 9 16 25 36 49 64 81 100


=={{header|Dart}}==
=={{header|Dart}}==