100 doors: Difference between revisions
Content deleted Content added
Added Vala, fixed alphabetization |
|||
Line 460: | Line 460: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Batch File}}== |
|||
'''unoptimized''' |
|||
<lang dos> |
|||
@echo off |
|||
setlocal enableDelayedExpansion |
|||
:: 0 = closed |
|||
:: 1 = open |
|||
:: SET /A treats undefined variable as 0 |
|||
:: Negation operator ! must be escaped because delayed expansion is enabled |
|||
for /l %%p in (1 1 100) do for /l %%d in (%%p %%p 100) do set /a "door%%d=^!door%%d" |
|||
for /l %%d in (1 1 100) do if !door%%d!==1 ( |
|||
echo door %%d is open |
|||
) else echo door %%d is closed |
|||
</lang> |
|||
'''optimized''' |
|||
<lang dos> |
|||
@echo off |
|||
setlocal enableDelayedExpansion |
|||
set /a square=1, incr=3 |
|||
for /l %%d in (1 1 100) do ( |
|||
if %%d neq !square! (echo door %%d is closed) else ( |
|||
echo door %%d is open |
|||
set /a square+=incr, incr+=2 |
|||
) |
|||
) |
|||
</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Line 523: | Line 494: | ||
END IF |
END IF |
||
NEXT i</lang> |
NEXT i</lang> |
||
=={{header|Batch File}}== |
|||
'''unoptimized''' |
|||
<lang dos> |
|||
@echo off |
|||
setlocal enableDelayedExpansion |
|||
:: 0 = closed |
|||
:: 1 = open |
|||
:: SET /A treats undefined variable as 0 |
|||
:: Negation operator ! must be escaped because delayed expansion is enabled |
|||
for /l %%p in (1 1 100) do for /l %%d in (%%p %%p 100) do set /a "door%%d=^!door%%d" |
|||
for /l %%d in (1 1 100) do if !door%%d!==1 ( |
|||
echo door %%d is open |
|||
) else echo door %%d is closed |
|||
</lang> |
|||
'''optimized''' |
|||
<lang dos> |
|||
@echo off |
|||
setlocal enableDelayedExpansion |
|||
set /a square=1, incr=3 |
|||
for /l %%d in (1 1 100) do ( |
|||
if %%d neq !square! (echo door %%d is closed) else ( |
|||
echo door %%d is open |
|||
set /a square+=incr, incr+=2 |
|||
) |
|||
) |
|||
</lang> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
Line 1,565: | Line 1,565: | ||
(X, false)-> io:format("Door ~p: close~n",[X]) end, |
(X, false)-> io:format("Door ~p: close~n",[X]) end, |
||
[Out(X,F(X)) || X <- lists:seq(1,100)].</lang> |
[Out(X,F(X)) || X <- lists:seq(1,100)].</lang> |
||
=={{header|Euphoria}}== |
|||
unoptimised |
|||
<lang Euphoria>-- doors.ex |
|||
include std/console.e |
|||
sequence doors |
|||
doors = repeat( 0, 100 ) -- 1 to 100, initialised to false |
|||
for pass = 1 to 100 do |
|||
for door = pass to 100 by pass do |
|||
--printf( 1, "%d", doors[door] ) |
|||
--printf( 1, "%d", not doors[door] ) |
|||
doors[door] = not doors[door] |
|||
end for |
|||
end for |
|||
sequence oc |
|||
for i = 1 to 100 do |
|||
if doors[i] then |
|||
oc = "open" |
|||
else |
|||
oc = "closed" |
|||
end if |
|||
printf( 1, "door %d is %s\n", { i, oc } ) |
|||
end for |
|||
</lang> |
|||
=={{header|Euler Math Toolbox}}== |
=={{header|Euler Math Toolbox}}== |
||
Line 1,623: | Line 1,596: | ||
door 100 is open |
door 100 is open |
||
</lang> |
</lang> |
||
=={{header|Euphoria}}== |
|||
unoptimised |
|||
<lang Euphoria>-- doors.ex |
|||
include std/console.e |
|||
sequence doors |
|||
doors = repeat( 0, 100 ) -- 1 to 100, initialised to false |
|||
for pass = 1 to 100 do |
|||
for door = pass to 100 by pass do |
|||
--printf( 1, "%d", doors[door] ) |
|||
--printf( 1, "%d", not doors[door] ) |
|||
doors[door] = not doors[door] |
|||
end for |
|||
end for |
|||
sequence oc |
|||
for i = 1 to 100 do |
|||
if doors[i] then |
|||
oc = "open" |
|||
else |
|||
oc = "closed" |
|||
end if |
|||
printf( 1, "door %d is %s\n", { i, oc } ) |
|||
end for |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
|||
Requires #light in versions of F# prior to 2010 beta. |
|||
<lang fsharp>let answerDoors = |
|||
let ToggleNth n (lst:bool array) = // Toggle every n'th door |
|||
[(n-1) .. n .. 99] // For each appropriate door |
|||
|> Seq.iter (fun i -> lst.[i] <- not lst.[i]) // toggle it |
|||
let doors = Array.create 100 false // Initialize all doors to closed |
|||
Seq.iter (fun n -> ToggleNth n doors) [1..100] // toggle the appropriate doors for each pass |
|||
doors // Initialize all doors to closed |
|||
</lang> |
|||
Following is the solution using perfect squares. The coercions in PerfectSquare are, I believe, slightly different in versions prior to 2010 beta and, again, #light is required in those versions. |
|||
<lang fsharp>open System |
|||
let answer2 = |
|||
let PerfectSquare n = |
|||
let sqrt = int(Math.Sqrt(float n)) |
|||
n = sqrt * sqrt |
|||
[| for i in 1..100 do yield PerfectSquare i |]</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 1,759: | Line 1,777: | ||
END PROGRAM DOORS</lang> |
END PROGRAM DOORS</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
Requires #light in versions of F# prior to 2010 beta. |
|||
<lang fsharp>let answerDoors = |
|||
let ToggleNth n (lst:bool array) = // Toggle every n'th door |
|||
[(n-1) .. n .. 99] // For each appropriate door |
|||
|> Seq.iter (fun i -> lst.[i] <- not lst.[i]) // toggle it |
|||
let doors = Array.create 100 false // Initialize all doors to closed |
|||
Seq.iter (fun n -> ToggleNth n doors) [1..100] // toggle the appropriate doors for each pass |
|||
doors // Initialize all doors to closed |
|||
</lang> |
|||
Following is the solution using perfect squares. The coercions in PerfectSquare are, I believe, slightly different in versions prior to 2010 beta and, again, #light is required in those versions. |
|||
<lang fsharp>open System |
|||
let answer2 = |
|||
let PerfectSquare n = |
|||
let sqrt = int(Math.Sqrt(float n)) |
|||
n = sqrt * sqrt |
|||
[| for i in 1..100 do yield PerfectSquare i |]</lang> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
<lang gap>doors := function(n) |
<lang gap>doors := function(n) |
||
Line 2,268: | Line 2,269: | ||
;Optimized |
;Optimized |
||
{{VI solution|LabVIEW_100_doors.png}} |
{{VI solution|LabVIEW_100_doors.png}} |
||
=={{header|Liberty BASIC}}== |
|||
<lang lb>dim doors(100) |
|||
for pass = 1 to 100 |
|||
for door = pass to 100 step pass |
|||
doors(door) = not(doors(door)) |
|||
next door |
|||
next pass |
|||
print "open doors "; |
|||
for door = 1 to 100 |
|||
if doors(door) then print door;" "; |
|||
next door</lang> |
|||
=={{header|Logo}}== |
|||
<lang Logo>to doors |
|||
;Problem 100 Doors |
|||
;FMSLogo |
|||
;lrcvs 2010 |
|||
make "door (vector 100 1) |
|||
for [p 1 100][setitem :p :door 0] |
|||
for [a 1 100 1][for [b :a 100 :a][make "x item :b :door |
|||
ifelse :x = 0 [setitem :b :door 1][setitem :b :door 0] ] ] |
|||
for [c 1 100][make "y item :c :door |
|||
ifelse :y = 0 [pr (list :c "Close)] [pr (list :c "Open)] ] |
|||
end</lang> |
|||
=={{header|Lhogho}}== |
=={{header|Lhogho}}== |
||
Line 2,332: | Line 2,303: | ||
doors</lang> |
doors</lang> |
||
=={{header|Liberty BASIC}}== |
|||
<lang lb>dim doors(100) |
|||
for pass = 1 to 100 |
|||
for door = pass to 100 step pass |
|||
doors(door) = not(doors(door)) |
|||
next door |
|||
next pass |
|||
print "open doors "; |
|||
for door = 1 to 100 |
|||
if doors(door) then print door;" "; |
|||
next door</lang> |
|||
=={{header|Logo}}== |
|||
<lang Logo>to doors |
|||
;Problem 100 Doors |
|||
;FMSLogo |
|||
;lrcvs 2010 |
|||
make "door (vector 100 1) |
|||
for [p 1 100][setitem :p :door 0] |
|||
for [a 1 100 1][for [b :a 100 :a][make "x item :b :door |
|||
ifelse :x = 0 [setitem :b :door 1][setitem :b :door 0] ] ] |
|||
for [c 1 100][make "y item :c :door |
|||
ifelse :y = 0 [pr (list :c "Close)] [pr (list :c "Open)] ] |
|||
end</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 2,534: | Line 2,535: | ||
endfor |
endfor |
||
end</lang> |
end</lang> |
||
=={{header|Mirah}}== |
|||
<lang Mirah>import java.util.ArrayList |
|||
class Door |
|||
:state |
|||
def initialize |
|||
@state=false |
|||
end |
|||
def closed?; !@state; end |
|||
def open?; @state; end |
|||
def close; @state=false; end |
|||
def open; @state=true; end |
|||
def toggle |
|||
if closed? |
|||
open |
|||
else |
|||
close |
|||
end |
|||
end |
|||
def toString; Boolean.toString(@state); end |
|||
end |
|||
doors=ArrayList.new |
|||
1.upto(100) do |
|||
doors.add(Door.new) |
|||
end |
|||
1.upto(100) do |multiplier| |
|||
index = 0 |
|||
doors.each do |door| |
|||
Door(door).toggle if (index+1)%multiplier == 0 |
|||
index += 1 |
|||
end |
|||
end |
|||
i = 0 |
|||
doors.each do |door| |
|||
puts "Door #{i+1} is #{door}." |
|||
i+=1 |
|||
end |
|||
</lang> |
|||
=={{header|MIPS Assembly}}== |
=={{header|MIPS Assembly}}== |
||
Line 2,647: | Line 2,600: | ||
addi $t1, $t1, 1 |
addi $t1, $t1, 1 |
||
bne $t1, 101 loop3 |
bne $t1, 101 loop3 |
||
</lang> |
|||
=={{header|Mirah}}== |
|||
<lang Mirah>import java.util.ArrayList |
|||
class Door |
|||
:state |
|||
def initialize |
|||
@state=false |
|||
end |
|||
def closed?; !@state; end |
|||
def open?; @state; end |
|||
def close; @state=false; end |
|||
def open; @state=true; end |
|||
def toggle |
|||
if closed? |
|||
open |
|||
else |
|||
close |
|||
end |
|||
end |
|||
def toString; Boolean.toString(@state); end |
|||
end |
|||
doors=ArrayList.new |
|||
1.upto(100) do |
|||
doors.add(Door.new) |
|||
end |
|||
1.upto(100) do |multiplier| |
|||
index = 0 |
|||
doors.each do |door| |
|||
Door(door).toggle if (index+1)%multiplier == 0 |
|||
index += 1 |
|||
end |
|||
end |
|||
i = 0 |
|||
doors.each do |door| |
|||
puts "Door #{i+1} is #{door}." |
|||
i+=1 |
|||
end |
|||
</lang> |
</lang> |
||
Line 3,138: | Line 3,139: | ||
WriteLn('Close doors: ' + ACloseDoors); |
WriteLn('Close doors: ' + ACloseDoors); |
||
end. </lang> |
end. </lang> |
||
=={{header|PHP}}== |
|||
'''optimized''' |
|||
<lang php><?php |
|||
for ($i = 1; $i <= 100; $i++) { |
|||
$root = sqrt($i); |
|||
$state = ($root == ceil($root)) ? 'open' : 'closed'; |
|||
echo "Door {$i}: {$state}\n"; |
|||
} |
|||
?></lang> |
|||
'''unoptimized''' |
|||
<lang php><?php |
|||
$toggleState = array('open' => 'closed', 'closed' => 'open'); |
|||
$doors = array_fill(1, 100, 'closed'); |
|||
for ($pass = 1; $pass <= 100; ++$pass) { |
|||
for ($nr = 1; $nr <= 100; ++$nr) { |
|||
if ($nr % $pass == 0) { |
|||
$doors[$nr] = $toggleState[$doors[$nr]]; |
|||
} |
|||
} |
|||
} |
|||
for ($nr = 1; $nr <= 100; ++$nr) |
|||
printf("Door %d is %s\n", $nr, $doors[$nr]); |
|||
?></lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 3,214: | Line 3,190: | ||
<lang perl6>say "Door $_ is ", <closed open>[.sqrt == .sqrt.floor] for 1..100;</lang> |
<lang perl6>say "Door $_ is ", <closed open>[.sqrt == .sqrt.floor] for 1..100;</lang> |
||
=={{header|PHP}}== |
|||
'''optimized''' |
|||
<lang php><?php |
|||
for ($i = 1; $i <= 100; $i++) { |
|||
$root = sqrt($i); |
|||
$state = ($root == ceil($root)) ? 'open' : 'closed'; |
|||
echo "Door {$i}: {$state}\n"; |
|||
} |
|||
?></lang> |
|||
'''unoptimized''' |
|||
<lang php><?php |
|||
$toggleState = array('open' => 'closed', 'closed' => 'open'); |
|||
$doors = array_fill(1, 100, 'closed'); |
|||
for ($pass = 1; $pass <= 100; ++$pass) { |
|||
for ($nr = 1; $nr <= 100; ++$nr) { |
|||
if ($nr % $pass == 0) { |
|||
$doors[$nr] = $toggleState[$doors[$nr]]; |
|||
} |
|||
} |
|||
} |
|||
for ($nr = 1; $nr <= 100; ++$nr) |
|||
printf("Door %d is %s\n", $nr, $doors[$nr]); |
|||
?></lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 3,833: | Line 3,834: | ||
<lang Salmon>variable y:=1;for(x;1;x<101)"Door "~sprint(x)~" is "~(x==y*y?{++y;return"open";}:"closed")!;</lang> |
<lang Salmon>variable y:=1;for(x;1;x<101)"Door "~sprint(x)~" is "~(x==y*y?{++y;return"open";}:"closed")!;</lang> |
||
=={{header|Sather}}== |
|||
<lang sather>class MAIN is |
|||
main is |
|||
pass, door :INT; |
|||
doors :ARRAY{BOOL} := #(100); |
|||
loop |
|||
doors[0.upto!(99)] := false; |
|||
end; |
|||
pass := 0; |
|||
loop while!(pass < 100); |
|||
door := pass; |
|||
loop while! (door < 100); |
|||
doors[door] := ~doors[door]; |
|||
door := door + pass + 1 |
|||
end; |
|||
pass := pass + 1; |
|||
end; |
|||
loop |
|||
door := 0.upto!(99); |
|||
#OUT + (door+1) + " " + doors[door] + "\n"; |
|||
end; |
|||
end; |
|||
end;</lang> |
|||
=={{header|SAS}}== |
=={{header|SAS}}== |
||
Line 3,888: | Line 3,865: | ||
println("open: " + o) |
println("open: " + o) |
||
println("closed: " + (1 to 100 filterNot o.contains))</lang> |
println("closed: " + (1 to 100 filterNot o.contains))</lang> |
||
=={{header|Sather}}== |
|||
<lang sather>class MAIN is |
|||
main is |
|||
pass, door :INT; |
|||
doors :ARRAY{BOOL} := #(100); |
|||
loop |
|||
doors[0.upto!(99)] := false; |
|||
end; |
|||
pass := 0; |
|||
loop while!(pass < 100); |
|||
door := pass; |
|||
loop while! (door < 100); |
|||
doors[door] := ~doors[door]; |
|||
door := door + pass + 1 |
|||
end; |
|||
pass := pass + 1; |
|||
end; |
|||
loop |
|||
door := 0.upto!(99); |
|||
#OUT + (door+1) + " " + doors[door] + "\n"; |
|||
end; |
|||
end; |
|||
end;</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 4,036: | Line 4,037: | ||
96 is closed 97 is closed 98 is closed 99 is closed 100 is open |
96 is closed 97 is closed 98 is closed 99 is closed 100 is open |
||
</pre> |
</pre> |
||
=={{header|SETL}}== |
|||
'''Unoptimized''' |
|||
<lang setl>program hundred_doors; |
|||
const toggle := {['open', 'closed'], ['closed', 'open']}; |
|||
doorStates := ['closed'] * 100; |
|||
(for interval in [1..100]) |
|||
doorStates := [if i mod interval = 0 then |
|||
toggle(prevState) else |
|||
prevState end: |
|||
prevState = doorStates(i)]; |
|||
end; |
|||
(for finalState = doorStates(i)) |
|||
print('door', i, 'is', finalState); |
|||
end; |
|||
end program;</lang> |
|||
If 'open' weren't a reserved word, we could omit the single quotes around it. |
|||
'''Optimized''' |
|||
Exploits the fact that squares are separated by successive odd numbers. Use array replication to insert the correct number of closed doors in between the open ones. |
|||
<lang setl>program hundred_doors; |
|||
doorStates := (+/ [['closed'] * oddNum with 'open': oddNum in [1,3..17]]); |
|||
(for finalState = doorStates(i)) |
|||
print('door', i, 'is', finalState); |
|||
end; |
|||
end program;</lang> |
|||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
Line 4,120: | Line 4,155: | ||
delay wait]]] fork. |
delay wait]]] fork. |
||
</lang> |
</lang> |
||
=={{header|SETL}}== |
|||
'''Unoptimized''' |
|||
<lang setl>program hundred_doors; |
|||
const toggle := {['open', 'closed'], ['closed', 'open']}; |
|||
doorStates := ['closed'] * 100; |
|||
(for interval in [1..100]) |
|||
doorStates := [if i mod interval = 0 then |
|||
toggle(prevState) else |
|||
prevState end: |
|||
prevState = doorStates(i)]; |
|||
end; |
|||
(for finalState = doorStates(i)) |
|||
print('door', i, 'is', finalState); |
|||
end; |
|||
end program;</lang> |
|||
If 'open' weren't a reserved word, we could omit the single quotes around it. |
|||
'''Optimized''' |
|||
Exploits the fact that squares are separated by successive odd numbers. Use array replication to insert the correct number of closed doors in between the open ones. |
|||
<lang setl>program hundred_doors; |
|||
doorStates := (+/ [['closed'] * oddNum with 'open': oddNum in [1,3..17]]); |
|||
(for finalState = doorStates(i)) |
|||
print('door', i, 'is', finalState); |
|||
end; |
|||
end program;</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Line 4,538: | Line 4,539: | ||
<1,4,9,16,25,36,49,64,81> |
<1,4,9,16,25,36,49,64,81> |
||
</pre> |
</pre> |
||
=={{header|Vala}}== |
|||
'''Unoptimized''' |
|||
<lang vala>int main() { |
|||
bool doors_open[101]; |
|||
for(int i = 1; i < doors_open.length; i++) { |
|||
for(int j = 1; i*j < doors_open.length; j++) { |
|||
doors_open[i*j] = !doors_open[i*j]; |
|||
} |
|||
stdout.printf("%d: %s\n", i, (doors_open[i] ? "open" : "closed")); |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
Output: |
|||
<pre>1: open |
|||
2: closed |
|||
3: closed |
|||
4: open |
|||
5: closed |
|||
6: closed |
|||
7: closed |
|||
8: closed |
|||
9: open |
|||
10: closed |
|||
11: closed |
|||
...</pre> |
|||
'''Optimized''' |
|||
<lang vala>int main() { |
|||
int i = 1; |
|||
while(i*i <= 100) { |
|||
stdout.printf("${i*i} open\n"); |
|||
i++; |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
Output: |
|||
<pre>1 open |
|||
4 open |
|||
9 open |
|||
16 open |
|||
25 open |
|||
36 open |
|||
49 open |
|||
64 open |
|||
81 open |
|||
100 open</pre> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |