100 doors: Difference between revisions

181 bytes added ,  12 years ago
m
→‎{{header|REXX}}: added comments, add DO-END labels, removed blank lines. -- ~~~~
(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/*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. */
 
<lang rexx>
/*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 /*process a pass-through for all doors.*/
do k=j by j to doors /* ... every Jth door from this point. */
door.k=\door.k /*toggle the "openness" of the door. */
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'''
end
 
say
</lang>
Output:
<pre style="height:30ex;overflow:scroll">
 
After 100 passes, the following doors are open:
 
Line 3,606 ⟶ 3,599:
81
100
 
</pre>
Here===version is another version3, solving it the easy way=== (version 1).
Here is another version, solving it the easy way.
<lang rexx>
<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
do j=1 for doors while j*j<=doors /*limitprocess thean easy pass-throughsthrough. */
 
do j p=1j*j for doors /*processsquare anthe easydoor pass-throughnumber. */
p=j*j /*square the door number. */
/*An alternative: P=J**2 */
if p>doors then leave /*if too large, we're done. */
say right(p,20)
end /*j*/</lang>
'''output'''
 
say
</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.*/
<lang rexx>
/*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'''
end
 
say
</lang>
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 />