100 doors: Difference between revisions

1,254 bytes added ,  17 days ago
m (→‎{{header|Oberon}}: Fixed language name and use Modula-2 for syntax highlighting)
(8 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≠⌿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 7,901 ⟶ 7,903:
=={{header|langur}}==
=== not optimized ===
<syntaxhighlight lang="langur">var .doors = [false] * 100
 
for .i of .doors {
for .j = .i; .j <= len(.doors); .j += .i {
.doors[.j] = not .doors[.j]
}
}
 
writeln for[=[]] .i of .doors { if(. doors[.i]: _for ~= [.more(_for, i]) }</syntaxhighlight>
END Doors. </syntaxhighlight>
 
Or, we could use the foldfrom() function to produce the output.
<syntaxhighlight lang="langur">writeln foldfrom(fn(. a, .b, .c): if(.b: .a~[.c]; .a), [], .doors, series (1..len .(doors)</syntaxhighlight>))
</syntaxhighlight>
 
=== optimized ===
<syntaxhighlight lang="langur">writeln map (fn{^2}, 1..10</syntaxhighlight>)
</syntaxhighlight>
 
{{out}}
Line 9,207 ⟶ 9,212:
=={{header|Oberon-07}}==
[http://oberon07.com/ Oberon-07], by [http://people.inf.ethz.ch/wirth/index.html Niklaus Wirth].
<syntaxhighlight lang="modula2"> '''MODULE''' Doors;
'''IMPORT''' Out;
'''PROCEDURE''' Do*; ''(* In Oberon an asterisk after an identifier is an export mark *)''
'''CONST''' N = 100; len = N + 1;
'''VAR''' i, j: INTEGER;
closed: '''ARRAY''' len '''OF''' BOOLEAN; ''(* Arrays in Oberon always start with index 0; closed[0] is not used *)''
'''BEGIN'''
'''FOR''' i := 1 '''TO''' N '''DO''' closed[i] := '''TRUE''' '''END''';
'''FOR''' i := 1 '''TO''' N '''DO'''
j := 1;
'''WHILE''' j <&lt; len '''DO'''
'''IF''' j '''MOD''' i = 0 '''THEN''' closed[j] := ~closed[j] '''END'''; INC(j) ''(* ~ = NOT *)''
'''END'''
'''END''';
''(* Print a state diagram of all doors *)''
'''FOR''' i := 1 '''TO''' N '''DO'''
'''IF''' (i - 1) '''MOD''' 10 = 0 '''THEN''' Out.Ln '''END''';
'''IF''' closed[i] '''THEN''' Out.String("- ") '''ELSE''' Out.String("+ ") '''END'''
'''END'''; Out.Ln;
''(* Print the numbers of the open doors *)''
'''FOR''' i := 1 '''TO''' N '''DO'''
'''IF''' ~closed[i] '''THEN''' Out.Int(i, 0); Out.Char(" ") '''END'''
'''END'''; Out.Ln
'''END''' Do;
'''END''' Doors.
 
END Doors. </syntaxhighlight>
Execute: Doors.Do<br/>
{{out}}
Line 14,068 ⟶ 14,074:
IF @(d) PRINT "Door ";d;" is open"
NEXT d</syntaxhighlight>
 
=={{header|Uiua}}==
<pre>
◿2/+=0⊞◿.+1⇡100
+1⇡100 # 1-100
⊞◿. # Mod each with 1-100
=0 # Find where mod = 0, aka the divisors
/+ # Sum to get num of divisors
◿2 # Num divisors is odd
</pre>
{{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>
 
=={{header|Uniface}}==
Line 14,656 ⟶ 14,674:
</pre>
 
===Optimized Optimised GO Inspired===
<syntaxhighlight lang="go">const door_number = 100
 
Line 14,670 ⟶ 14,688:
increment++
door_nbr += 2 * increment + 1
print('O')
}
} else {
print('=')
}
}
doors.map( fn( it bool) bool { // graphically represent opened doors
print( if it {( 'O')} else {('=')} )
return it
})
println('')
}
Line 14,681 ⟶ 14,698:
Output:<pre>
O==O====O======O========O==========O============O==============O================O==================O
</pre>
 
===Optimized +===
<syntaxhighlight lang="go">fn main() {
for i in 1..11 {
print ( " Door ${i*i} is open.\n" )
}
}
</syntaxhighlight>
Output:<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>
 
1,007

edits