100 doors: Difference between revisions
m
→{{header|langur}}
imported>Grootson |
Langurmonkey (talk | contribs) |
||
(46 intermediate revisions by 23 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 905 ⟶ 944:
=={{header|ALGOL 60}}==
{{works with|A60}}
'''begin'''
''comment - 100 doors problem in ALGOL-60;''
'''boolean''' '''array''' doors[1:100];
'''integer''' i, j;
'''boolean''' open, closed;
open := '''true''';
closed := '''not''' '''true''';
outstring(1,"100 Doors Problem\n");
''comment - all doors are initially closed;''
'''for''' i := 1 '''step''' 1 '''until''' 100 '''do'''
doors[i] := closed;
''comment''
'' cycle through at increasing intervals''
'' and flip each door encountered;''
'''for''' i := 1 '''step''' 1 '''until''' 100 '''do'''
'''for''' j := i '''step''' i '''until''' 100 '''do'''
doors[j] := '''not''' doors[j];
''comment - show which doors are open;''
outstring(1,"The open doors are:");
'''for''' i := 1 '''step''' 1 '''until''' 100 '''do'''
'''if''' doors[i] '''then'''
outinteger(1,i);
'''end'''
{{out}}
<pre>
Line 946 ⟶ 984:
=={{header|ALGOL 68}}==
'''unoptimized'''
'''PROC''' doors = ('''INT''' limit)'''VOID''':
(
'''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)
'''optimized'''
'''PROC''' doors optimised = ( '''INT''' limit )'''VOID''':
'''FOR''' i '''TO''' limit '''DO'''
'''REAL''' num := sqrt(i);
print((whole(i,0)," is ",('''ENTIER''' num = num |"opened"|"closed"),newline))
'''OD'''
;
doors optimised(100)
=={{header|ALGOL W}}==
'''begin'''
'''integer''' doorMax;
doorMax := 100;
'''begin'''
''% need to start a new block so the array can have variable bounds %''
''% array of doors - door( i ) is true if open, false if closed %''
'''logical''' '''array''' door( 1 :: doorMax );
''% set all doors to closed %''
'''for''' i := 1 '''until''' doorMax '''do''' door( i ) := '''false''';
''% repeatedly flip the doors %''
'''for''' i := 1 '''until''' doorMax
'''do''' '''begin'''
'''for''' j := i '''step''' i '''until''' doorMax
'''do''' '''begin'''
door( j ) := '''not''' door( j )
'''end'''
'''end''';
''% display the results %''
i_w := 1; ''% set integer field width %''
s_w := 1; ''% and separator width %''
'''for''' i := 1 '''until''' doorMax '''do''' '''if''' door( i ) '''then''' writeon( i )
'''end'''
'''end'''.
{{out}}
<pre>
Line 1,080 ⟶ 1,117:
<syntaxhighlight lang="apl">doors←{100⍴((⍵-1)⍴0),1}
≠⌿⊃doors¨ ⍳100</syntaxhighlight>
'''optimized'''
Line 1,095 ⟶ 1,126:
{{works with|Dyalog APL}}
{{works with|GNU APL}}
<syntaxhighlight lang="apl">
≠⌿0=(⍳100)∘.|⍳100</syntaxhighlight>
Each of the above solutions produces the same output:
{{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>
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,918 ⟶ 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,128 ⟶ 2,201:
==={{header|Minimal BASIC}}===
{{works with|IS-BASIC}}
<syntaxhighlight lang="qbasic">10 PRINT "FOLLOWING DOORS ARE OPEN:"
20 LET I = 0
Line 2,135 ⟶ 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,206 ⟶ 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,218 ⟶ 2,324:
80 IF D(I) THEN PRINT I,
90 NEXT I</syntaxhighlight>
=={{header|Batch File}}==
Line 2,347 ⟶ 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 4,508 ⟶ 4,605:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
len d[] 100
for p = 1 to 100
i = p
while i <= 100
d[i] = 1 - d[i]
i += p
.
.
for i = 1 to 100
if d[i] = 1
print i
.
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Line 5,087 ⟶ 5,186:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 5,093 ⟶ 5,192:
public program()
{
var Doors := Array.allocate(100).populate::(n=>false);
for(int i := 0
{
for(int j := i
{
Doors[j] := Doors[j].Inverted
Line 5,102 ⟶ 5,201:
};
for(int i := 0
{
console.printLine("Door #",i + 1," :",Doors[i].iif("Open","Closed"))
Line 5,263 ⟶ 5,362:
Door:State state
new by int =id, Door:State =state do end
fun toggle ← <|me.state ← when(me.state æ Door:State.CLOSED, Door:State.OPEN, Door:State.CLOSED)
fun asText ← <|"Door #" + me.
end
type Main
^|There are 100 doors in a row that are all initially closed.|^
List doors
^|You make 100 passes by the doors.|^
for int pass
for int i
doors[i].toggle()
end
Line 5,284 ⟶ 5,376:
^|Which are open, which are closed?|^
for each Door door in doors
if door.state
writeLine(door)
end
Line 5,397 ⟶ 5,489:
=={{header|Euler}}==
In Euler, all variables have the value <code>undefined</code> until assigned another value. <code>isu x</code> returns <code>true</code> if x is currently undefined and the and/or operators short-circuit.
'''begin''' '''new''' doors; '''new''' i; '''label''' doorLoop; '''label''' outDoors;
doorLoop: '''if''' [ i <- i + 1 ] <= '''length''' doors '''then''' '''begin'''
'''new''' j; '''label''' flipLoop;
flipLoop: '''if''' [ j <- J + i ] <= '''length''' doors '''then''' '''begin'''
'''end''' '''else'''
'''end''' '''else'''
outDoors: '''if''' [ i <- i + 1 ] <= '''length''' doors '''then''' '''begin'''
'''end''' '''else'''
'''end''' $
=={{header|Euler Math Toolbox}}==
Line 6,192 ⟶ 6,282:
{{FormulaeEntry|page=https://formulae.org/?script=examples/100_doors}}
'''Solution'''
The solution consists in having a 100 element array, initialized with FALSE values. In each of the 100 rounds (controlled by a simple FOR-FROM-TO cycle), the values are flipped using a FOR-FROM-TO-STEP cycle. Finally the array is shown, using green colors for open doors, and red for closed ones. The resulting matrix is transposed in order to be shown horizontally.
Line 6,198 ⟶ 6,290:
The result of calling the function is:
[[File:Fōrmulæ - 100 doors 06.png]]
[[File:Fōrmulæ - 100 doors 02.png]]
[[File:Fōrmulæ - 100 doors 03.png]]
Line 6,651 ⟶ 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,664 ⟶ 6,791:
while((i*i) <= n)
{
Sys.
i++;
}
Line 6,911 ⟶ 7,038:
END MAIN
</syntaxhighlight>
=={{Header|Insitux}}==
<syntaxhighlight lang="insitux">
(var doors (times 100 false))
(for i (range 1 101)
i2 (range (dec i) 100 i)
(var! doors (set-at [i2] (! (i2 doors))))
(continue))
(-> (xmap vec doors)
(filter 1)
(map (comp 0 inc))
(join ", ")
@(str "open doors: "))
</syntaxhighlight>
{{out}}
<pre>
open doors: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
</pre>
=={{header|Io}}==
Line 7,424 ⟶ 7,574:
}
});</syntaxhighlight>
{{works with|SpiderMonkey}}(Firefox) But not most (if any) other JavaScript engines. Array comprehension (<code>[ for... ]</code>) is non-standard.
<syntaxhighlight lang="javascript">// Array comprehension style
[ for (i of Array.apply(null, { length: 100 })) i ].forEach((_, i) => {
Line 7,432 ⟶ 7,581:
if (sqrt === (sqrt | 0)) {
console.log("Door
}
});</syntaxhighlight>
Line 7,754 ⟶ 7,903:
=={{header|langur}}==
=== not optimized ===
<syntaxhighlight lang="langur">var .doors = [false] * 100
for .i of .doors {
Line 7,766 ⟶ 7,914:
Or, we could use the foldfrom() function to produce the output.
<syntaxhighlight lang="langur">writeln foldfrom(
=== optimized ===
<syntaxhighlight lang="langur">writeln map
{{out}}
Line 8,201 ⟶ 8,346:
end
a
</syntaxhighlight>
'''Optimized - Alternative'''
<syntaxhighlight lang="matlab">
doors = zeros(1,100); // 0: closed 1: open
for i = 1:100
doors(i:i:100) = 1-doors(i:i:100)
Line 9,061 ⟶ 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,278 ⟶ 11,424:
'''ultra-optimized''': ported from Julia version<br>
<syntaxhighlight lang="python">for i in range(1,
=={{header|Q}}==
Line 11,601 ⟶ 11,747:
repeat n len [if doors/:n [print n]]
</syntaxhighlight>
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Show 1 <Walk 1 <Doors>>>;
};
NDoors { = 100; };
Doors { = <Repeat <NDoors> Closed>; };
Repeat {
0 s.val = ;
s.N s.val = s.val <Repeat <- s.N 1> s.val> ;
};
Toggle {
1 Closed e.rest = Open e.rest;
1 Open e.rest = Closed e.rest;
s.N s.door e.rest = s.door <Toggle <- s.N 1> e.rest>;
};
Pass {
s.pass s.door e.doors, <Compare s.door <NDoors>>: '+'
= e.doors;
s.pass s.door e.doors
= <Pass s.pass <+ s.pass s.door> <Toggle s.door e.doors>>;
};
Walk {
s.pass e.doors, <Compare s.pass <NDoors>>: '+'
= e.doors;
s.pass e.doors
= <Walk <+ s.pass 1> <Pass s.pass s.pass e.doors>>;
};
Show {
s.N Open e.rest = <Prout Door s.N is open>
<Show <+ s.N 1> e.rest>;
s.N Closed e.rest = <Show <+ s.N 1> e.rest>;
s.N = ;
};</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|Relation}}==
Line 13,194 ⟶ 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,251 ⟶ 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,790 ⟶ 14,019:
100 = open
</pre>
=={{header|TypeScript}}==
Line 14,605 ⟶ 14,810:
=={{header|Wren}}==
'''Unoptimized'''
<syntaxhighlight lang="
for (i in 1..100) {
var j = i
Line 14,620 ⟶ 14,825:
'''Optimized'''
<syntaxhighlight lang="
var increment = 3
while (door <= 100) {
Line 14,883 ⟶ 15,088:
Optimized
<syntaxhighlight lang="yabasic">for i = 1 to sqrt(100) : print "Door ", i**2, " is open" : next</syntaxhighlight>
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
!yamlscript/v0
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
defn doors():
reduce:
fn(doors idx): assoc(doors idx true)
into []: repeat(100 false)
map \(dec (%1 * %1)): 1 .. 10
</syntaxhighlight>
{{out}}
<pre>
$ ys 100-doors.ys
Open doors after 100 passes: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
</pre>
=={{header|Yorick}}==
Line 14,946 ⟶ 15,177:
===Optimized with new for-loop (since Zig 0.11)===
<syntaxhighlight lang="zig">
pub fn main() !void {
const stdout = @import("std").io.getStdOut().writer();
var square: u8 = 1;
var increment: u8 = 3;
Line 14,961 ⟶ 15,192:
</syntaxhighlight>
===
<syntaxhighlight lang="zig">
pub fn main() !void {
const stdout = @import("std").io.getStdOut().writer();
var door: u8 = 1;
while (door * door <= 100) : (door += 1) {
Line 14,971 ⟶ 15,202:
}
}
</syntaxhighlight>{{out}}
<pre>Door 1 is open.
|