Josephus problem: Difference between revisions

Content deleted Content added
Walterpachl (talk | contribs)
REXX added
Walterpachl (talk | contribs)
→‎{{header|REXX}}: generalized + extended
Line 146:
 
=={{header|REXX}}==
<lang rexx>/* REXX ***************************************************************
* 15.11.2012 Walter Pachl - my own solution
* 16.11.2012 Walter Pachl generalized n prisoners + w killing distance
* and s=number of survivors
**********************************************************************/
dead.=0 /* nobody's dead yet */
n=41 /* number of alive prisoners */
nn=n /* wrap around boundary */
w=3 /* killing count */
s=1 /* nuber of survivors */
p=-1 /* start here */
killed='' /* output of killings */
Do until n=1s /* until one alive prisoner */
found=0 /* start looking */
Do Until found=3w /* until we hasvehave the third */
p=p+1 /* next position */
If p=41nn Then p=0 /* wrap around */
If dead.p=0 Then /* a prisoner who is alive */
found=found+1 /* increment found count */
Line 167 ⟶ 172:
Say 'killed:'subword(killed,1,20) /* output killing sequence */
Say ' 'subword(killed,21) /* output killing sequence */
DoSay i=0'Survivor(s):' To 40 /* show /* look for the surviving p. */
Do If dead.i=0 ThenTo Leave40 /* found him /* look for the surviving p's */
Say 'Prisoner' If dead.i=0 'survives'Then Say i /* show /* found one */</lang>
End</lang>
Say 'Prisoner' i 'survives' /* show */</lang>
Output:
<pre>
Line 176 ⟶ 181:
31 36 40 6 12 19 25 33 39 7 16 28 37 10 24 1 21 3 34 15
Prisoner 30 survives
</pre>
 
=={{header|Scala}}==