100 doors: Difference between revisions

4,008 bytes added ,  18 days ago
m
(Add Refal)
 
(19 intermediate revisions by 12 users not shown)
Line 594:
NEXT list = VALUE #( BASE list ( i * i ) ) ) ).
</syntaxhighlight>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO INITIALIZE:
SHARE doors
PUT {} IN doors
FOR door IN {1..100}:
PUT 0 IN doors[door]
 
HOW TO TOGGLE door:
SHARE doors
PUT 1-doors[door] IN doors[door]
 
HOW TO WALK step:
SHARE doors
PUT step IN door
WHILE door <= 100:
TOGGLE door
PUT door+step IN door
 
HOW TO DISPLAY OPEN DOORS:
SHARE doors
FOR door IN {1..100}:
IF doors[door] = 1:
WRITE "Door", door, "is open"/
 
INITIALIZE
FOR pass IN {1..100}: WALK pass
DISPLAY OPEN DOORS</syntaxhighlight>
{{out}}
<pre>Door 1 is open
Door 4 is open
Door 9 is open
Door 16 is open
Door 25 is open
Door 36 is open
Door 49 is open
Door 64 is open
Door 81 is open
Door 100 is open</pre>
 
=={{header|ACL2}}==
Line 945 ⟶ 984:
=={{header|ALGOL 68}}==
'''unoptimized'''
<syntaxhighlight lang="algol68">
PROC doors = (INT limit)VOID:
(
MODE DOORSTATE = BOOL;
BOOL closed = FALSE;
BOOL open = NOT closed;
MODE DOORLIST = [limit]DOORSTATE;
 
'''PROC''' doors = ('''INT''' limit)'''VOID''':
DOORLIST the doors;
(
FOR i FROM LWB the doors TO UPB the doors DO the doors[i]:=closed OD;
'''MODE''' '''DOORSTATE''' = '''BOOL''';
'''BOOL''' closed = '''FALSE''';
'''BOOL''' open = '''NOT''' closed;
'''MODE''' '''DOORLIST''' = [limit]'''DOORSTATE''';
'''DOORLIST''' the doors;
'''FOR''' i '''FROM''' '''LWB''' the doors '''TO''' '''UPB''' the doors '''DO''' the doors[i]:=closed '''OD''';
'''FOR''' i '''FROM''' '''LWB''' the doors '''TO''' '''UPB''' the doors '''DO'''
'''FOR''' j '''FROM''' '''LWB''' the doors '''BY''' i '''TO''' '''UPB''' the doors '''DO'''
the doors[j] := '''NOT''' the doors[j]
'''OD'''
'''OD''';
'''FOR''' i '''FROM''' '''LWB''' the doors '''TO''' '''UPB''' the doors '''DO'''
print((whole(i,-12)," is ",(the doors[i]|"opened"|"closed"),newline))
'''OD'''
);
doors(100)
 
FOR i FROM LWB the doors TO UPB the doors DO
FOR j FROM LWB the doors BY i TO UPB the doors DO
the doors[j] := NOT the doors[j]
OD
OD;
FOR i FROM LWB the doors TO UPB the doors DO
print((whole(i,-12)," is ",(the doors[i]|"opened"|"closed"),newline))
OD
);
doors(100)
</syntaxhighlight>
'''optimized'''
 
<syntaxhighlight lang="algol68">PROC doors optimised = ( INT limit )VOID:
'''PROC''' doors optimised = ( '''INT''' limit )'''VOID''':
FOR i TO limit DO
'''FOR''' i '''TO''' limit '''DO'''
REAL num := sqrt(i);
'''REAL''' num := sqrt(i);
printf(($g" is "gl$,i,(ENTIER num = num |"opened"|"closed") ))
print((whole(i,0)," is ",('''ENTIER''' num = num |"opened"|"closed"),newline))
OD
'''OD'''
;
;
doors optimised(limit)</syntaxhighlight>
doors optimised(100)
 
=={{header|ALGOL W}}==
Line 1,077 ⟶ 1,117:
<syntaxhighlight lang="apl">doors←{100⍴((⍵-1)⍴0),1}
≠⌿⊃doors¨ ⍳100</syntaxhighlight>
{{out}}
<pre>
1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
</pre>
 
'''optimized'''
Line 1,092 ⟶ 1,126:
 
{{works with|Dyalog APL}}
{{works with|GNU APL}}
<syntaxhighlight lang="apl">
≠⌿0=(⍳100)∘.|⍳100</syntaxhighlight>
⍝⍝ Also works with GNU APL after introduction of
 
⍝⍝ the ⍸ function with SVN r1368, Dec 03 2020
Each of the above solutions produces the same output:
⍸≠⌿0=(⍳100)∘.|⍳100</syntaxhighlight>
{{out}}
<pre>
1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 4 9 16 25 36 49 64 81 100
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
</pre>
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1</pre>
 
However the result is obtained, applying the ⍸ function (which has been in Dyalog since 16.0 and was added to GNU APL in SVN r1368, 2020-12-03) will transform the Boolean array into a list of the indices of the true values (open doors):
 
<syntaxhighlight lang="apl">⍸≠⌿0=(⍳100)∘.|⍳100</syntaxhighlight>
{{out}}
<pre>1 4 9 16 25 36 49 64 81 100</pre>
 
=={{header|AppleScript}}==
Line 1,915 ⟶ 1,957:
return(0);
}</syntaxhighlight>
 
=={{header|BabyCobol}}==
<syntaxhighlight lang="cobol">
* NB: the implementation is rather vanilla
* besides using the idiomatic buffer overrun.
* LOOP is what PERFORM in COBOL is, with defaults.
* MOVE in this language acts like OVE CORRESPONDING,
* which is actually good here.
IDENTIFICATION DIVISION.
PROGRAM-ID. ONE HUNDRED DOORS.
DATA DIVISION.
01 I PICTURE IS 9(3).
01 J LIKE I.
01 DOOR PICTURE IS 9 OCCURS 100 TIMES.
01 STOP LIKE DOOR.
PROCEDURE DIVISION.
* Initialise the data
MOVE HIGH-VALUES TO STOP
MOVE SPACES TO DOOR.
* Do the main algorithm
LOOP VARYING I UNTIL DOOR(I) = 9
LOOP VARYING J FROM I TO 100 BY I
SUBTRACT DOOR (J) FROM 1 GIVING DOOR (J)
END
END.
* Print the results
LOOP VARYING I UNTIL DOOR(I) = 9
DISPLAY "Door" I "is" WITH NO ADVANCING
IF DOOR (I) = 1
THEN DISPLAY "open"
ELSE DISPLAY "closed".
END.
</syntaxhighlight>
 
 
=={{header|BaCon}}==
Line 2,133 ⟶ 2,209:
60 IF I * I < 100 THEN 30
70 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
Based on the Sinclair ZX81 BASIC implementation.
<syntaxhighlight lang="basic">
10 DIM D(100)
20 FOR I=1 TO 100
30 FOR J=i TO 100 STEP I
40 D(J)=NOT D(J)
50 NEXT J
60 NEXT I
70 FOR I=1 TO 100
80 IF D(I) THEN PRINT I;
90 NEXT I
100 END
</syntaxhighlight>
{{out}}
<pre>
]RUN
1 4 9 16 25 36 49 64 81 100</pre>
 
===QBasic===
Line 2,204 ⟶ 2,299:
180 NEXT I
190 END</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="tiny basic"> PRINT "Open doors are:"
 
LET I = 1
10 IF I = 100 THEN END
rem funcion SQR
LET B = I*I
rem funcion MODULO
LET A = I - (I / B) * B
IF A < 11 THEN PRINT B
LET I = I + 1
GOTO 10</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 2,216 ⟶ 2,324:
80 IF D(I) THEN PRINT I,
90 NEXT I</syntaxhighlight>
 
==={{header|MSX Basic}}===
Based on the Sinclair ZX81 BASIC implementation.
<syntaxhighlight lang="basic">
10 DIM D(100)
20 FOR I=1 TO 100
30 FOR J=i TO 100 STEP I
40 D(J)=NOT D(J)
50 NEXT J
60 NEXT I
70 FOR I=1 TO 100
80 IF D(I) THEN PRINT I;
90 NEXT I
100 END
</syntaxhighlight>
{{out}}
<pre>
]RUN
1 4 9 16 25 36 49 64 81 100</pre>
 
=={{header|Batch File}}==
Line 2,345 ⟶ 2,434:
;::+1<r]!g9;>$08g1+:08paa[
*`#@_^._aa</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
This computes the characteristic sequence of squares by flipping every i'th door in round i, for infinitely many rounds i. But since it's computed lazily and the prefix stabilizes, we can still take the first 100 bits and print them! See corresponding source code at https://github.com/tromp/AIT/blob/master/characteristic_sequences/squares.lam
 
<pre>0001000100010101000110100000010110000011001110110010100011010000000000101111111000000101111101011001011001000110100001111100110100101111101111000000001011111111110110011001111111011100000000101111110000001011111010110011011100101011000000101111011001011110011110011110110100000000001011011100111011110000000001000000111001110100000000101101110110</pre>
 
Output
 
<pre>1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000001</pre>
 
=={{header|Blade}}==
Line 6,646 ⟶ 6,745:
 
=={{header|Haxe}}==
 
===Unoptimised===
<syntaxhighlight lang="haxe">
class Main
{
static public function main()
{
findOpenDoors( 100 );
}
 
static function findOpenDoors( n : Int )
{
var door = [];
for( i in 0...n + 1 ){ door[ i ] = false; }
for( i in 1...n + 1 ){
var j = i;
while( j <= n ){
door[ j ] = ! door[ j ];
j += i;
}
}
for( i in 1...n + 1 ){
if( door[ i ] ){ Sys.print( ' $i' ); }
}
}
}</syntaxhighlight>
{{out}}
<pre>
1 4 9 16 25 36 49 64 81 100
</pre>
 
===Optimised===
 
<syntaxhighlight lang="haxe">class RosettaDemo
{
Line 6,659 ⟶ 6,791:
while((i*i) <= n)
{
Sys.printprintln(i*i + "\n");
i++;
}
Line 7,771 ⟶ 7,903:
=={{header|langur}}==
=== not optimized ===
<syntaxhighlight lang="langur">var .doors = [false] * 100
{{works with|langur|0.8}}
<syntaxhighlight lang="langur">var .doors = arr 100, false
 
for .i of .doors {
Line 7,783 ⟶ 7,914:
 
Or, we could use the foldfrom() function to produce the output.
<syntaxhighlight lang="langur">writeln foldfrom(ffn(.a, .b, .c) { if(.b: .a~[.c]; .a) }, [], .doors, series 1..len .doors)</syntaxhighlight>
 
=== optimized ===
<syntaxhighlight lang="langur">writeln map(f .x fn{^ 2}, series 1..10)</syntaxhighlight>
 
{{works with|langur|0.8.11}}
<syntaxhighlight lang="langur">writeln map f{^2}, 1..10</syntaxhighlight>
 
{{out}}
Line 9,079 ⟶ 9,207:
echo outputString</syntaxhighlight>
 
=={{header|Oberon-07}}==
[http://oberon07.com/ Oberon-07], by [http://people.inf.ethz.ch/wirth/index.html Niklaus Wirth].
<syntaxhighlight lang="oberonmodula2">MODULE Doors;
IMPORT Out;
Line 11,296 ⟶ 11,424:
 
'''ultra-optimized''': ported from Julia version<br>
<syntaxhighlight lang="python">for i in range(1,10111): print("Door %s is open" % i**2)</syntaxhighlight>
 
=={{header|Q}}==
Line 13,263 ⟶ 13,391:
| 100 |
+-------+</syntaxhighlight>
 
=={{header|Stringle}}==
<syntaxhighlight lang="stringle">d "."
#d
i d
#i
p "door" #i
*p *p "."
i d f "oc"
i d #@f #*p
i d .\f "o" $ #i
i i d
#i +101 i ""
#i
d d "."
#d +101 d ""
#d</syntaxhighlight>
{{out}}
<pre>1
4
9
16
25
36
49
64
81
100</pre>
 
=={{header|SuperCollider}}==
Line 13,320 ⟶ 13,476:
println("Door \(index+1) is \(item.rawValue)")
}</syntaxhighlight>
===One-liner===
<syntaxhighlight lang="swift">
var arr: [Bool] = Array(1...100).map{ remquo(exp(log(Float($0))/2.0),1).0 == 0 }
</syntaxhighlight>
 
=={{header|Tailspin}}==
Line 13,859 ⟶ 14,019:
100 = open
</pre>
 
=={{Header|Tiny BASIC}}==
<syntaxhighlight lang="tiny basic"> PRINT "Open doors are:"
 
LET I = 1
10 IF I = 100 THEN END
rem funcion SQR
LET B = I*I
rem funcion MODULO
LET A = I - (I / B) * B
IF A < 11 THEN PRINT B
LET I = I + 1
GOTO 10</syntaxhighlight>
 
=={{header|Tiny Craft Basic}}==
<syntaxhighlight lang="basic">10 rem loop
 
20 let i = i + 1
30 print i * i, " open"
 
40 if i * i < 100 then 10
 
50 shell "pause"
60 end</syntaxhighlight>
 
=={{header|TypeScript}}==
Line 14,959 ⟶ 15,095:
 
defn main():
say: |-
"Open doors after 100 passes:
$(apply str interpose(\"', \"' open-doors()))"
 
defn open-doors():
for: .[[d n] map(vector doors() iterate(inc 1)) :when d] n:
n
 
defn doors():
reduce:
fn [(doors idx]): assoc(doors idx true)
into []: repeat(100 false)
map \(dec (%1 * %1)): 1 .. 10
</syntaxhighlight>
{{out}}
885

edits