100 doors: Difference between revisions
m
→{{header|langur}}
mNo edit summary |
Langurmonkey (talk | contribs) |
||
(7 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}}
<syntaxhighlight lang="apl">
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
▲ 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):
▲{{out}}
<pre>1 4 9 16 25 36 49 64 81 100</pre>
=={{header|AppleScript}}==
Line 6,743 ⟶ 6,745:
=={{header|Haxe}}==
===Unoptimised===
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,756 ⟶ 6,791:
while((i*i) <= n)
{
Sys.
i++;
}
Line 7,868 ⟶ 7,903:
=={{header|langur}}==
=== not optimized ===
▲{{works with|langur|0.8}}
▲<syntaxhighlight lang="langur">var .doors = [false] x 100
for .i of .doors {
Line 7,880 ⟶ 7,914:
Or, we could use the foldfrom() function to produce the output.
<syntaxhighlight lang="langur">writeln foldfrom(
=== optimized ===
<syntaxhighlight lang="langur">writeln map
▲<syntaxhighlight lang="langur">writeln map f{^2}, 1..10</syntaxhighlight>
{{out}}
Line 9,176 ⟶ 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="
IMPORT Out;
Line 11,393 ⟶ 11,424:
'''ultra-optimized''': ported from Julia version<br>
<syntaxhighlight lang="python">for i in range(1,
=={{header|Q}}==
Line 13,362 ⟶ 13,393:
=={{header|Stringle}}==
<syntaxhighlight lang="stringle">
#d
#i
p "door" #i
*p *p "."
i d #@f #*p▼
i d .\f "o" $ #i▼
i i d
#i
▲i d "."
#d +100 y 0▼
d d "."
#d</syntaxhighlight>
▲#@f #*p
▲.\f "o" $ #i
▲#i +100 x 0
▲x</syntaxhighlight>
{{out}}
<pre>1
|