100 doors: Difference between revisions

1,033 bytes added ,  1 month ago
m
(→‎{{header|ALGOL 68}}: Use Mediawiki markup for syntax highlighting, bugfix in the optimised version)
(9 intermediate revisions by 5 users not shown)
Line 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,132 ⟶ 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 2,207 ⟶ 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,278 ⟶ 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,290 ⟶ 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 6,730 ⟶ 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,743 ⟶ 6,791:
while((i*i) <= n)
{
Sys.printprintln(i*i + "\n");
i++;
}
Line 7,855 ⟶ 7,903:
=={{header|langur}}==
=== not optimized ===
<syntaxhighlight lang="langur">var .doors = [false] * 100
{{works with|langur|0.8}}
<syntaxhighlight lang="langur">var .doors = [false] x 100
 
for .i of .doors {
Line 7,867 ⟶ 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,163 ⟶ 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,380 ⟶ 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,347 ⟶ 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,947 ⟶ 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|TypeScript}}==
890

edits