100 doors: Difference between revisions
Content deleted Content added
Added Vala, fixed alphabetization |
m →{{header|REXX}}: added comments, add DO-END labels, removed blank lines. -- ~~~~ |
||
Line 3,552:
=={{header|REXX}}==
===version 1===
<lang rexx>
door. = 0
do inc = 1 to 100
do d = inc to 100 by inc
Line 3,563 ⟶ 3,565:
end
</lang>
===version 2, the hard way===
Here is another version, solving it the hard way.
<lang rexx>/*REXX program to solve the 100 door puzzle, the hard-way version. */▼
▲/*REXX program to solve the 100 door puzzle, the hard-way version. */
parse arg doors . /*get the first argument (# of doors.) */
if doors=='' then doors=100 /*not specified? Then assume 100 doors*/
/* 0 = closed. */
/* 1 = open. */
door.=0 /*assume all that all doors are closed.*/
do j=1 for doors
do k=j by j to doors
door.k=\door.k
end /*k*/
end /*j*/
say
say 'After' doors "passes, the following doors are open:"
say
do n=1 for doors▼
if door.n then say right(n,20)▼
▲ do n=1 for doors
end /*n*/</lang>
▲ if door.n then say right(n,20)
'''outut'''
<pre style="height:30ex;overflow:scroll">
After 100 passes, the following doors are open:
Line 3,606 ⟶ 3,599:
81
100
</pre>
Here is another version, solving it the easy way.
<lang rexx>/*REXX program to solve the 100 door puzzle, the easy-way version. */
parse arg doors . /*get the first argument (# of doors.) */
if doors=='' then doors=100 /*not specified? Then assume 100 doors*/
/* 0 = closed. */
/* 1 = open. */
Line 3,621 ⟶ 3,611:
say 'For the' doors "doors problem, the following doors are open:"
say
/*An alternative: P=J**2 */
if p>doors then leave
say right(p,20)
end /*j*/</lang>
'''output'''
<pre style="height:30ex;overflow:scroll">
For the 100 doors problem, the following doors are open:
Line 3,646 ⟶ 3,631:
81
100
</pre>
===version 4, easy way, 1,000 doors===
Here is another easy-way solution (version 2), but for 1,000 doors.
<lang rexx>/*REXX program to solve the 100 door puzzle, the easy-way version 2.*/▼
▲/*REXX program to solve the 100 door puzzle, the easy-way version 2.*/
parse arg doors . /*get the first argument (# of doors.) */
if doors=='' then doors=100 /*not specified? Then assume 100 doors*/
doors=1000
/* 0 = closed. */
Line 3,662 ⟶ 3,644:
say 'For the' doors "doors problem, the open doors are:"
say
do j=1 for doors while j*j<=doors /*limit the pass-throughs. */
say right(j**2,20)▼
▲ do j=1 for doors while j*j<=doors /*limit the pass-throughs. */
end /*j*/</lang>
▲ say right(j**2,20)
'''output'''
<pre style="height:30ex;overflow:scroll">
For the 1000 doors problem, the open doors are:
Line 3,705 ⟶ 3,682:
900
961
</pre>
=={{header|Ruby}}==
'''unoptimized; Ruby-way'''<br />
|