Josephus problem: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: generalized + extended)
m (REXX and Scala solution moved to follow Python solution)
Line 144: Line 144:
{{out}}
{{out}}
<pre>William survived.</pre>
<pre>William survived.</pre>

=={{header|Python}}==
<lang python>>>> def j(n, k):
p, i, seq = list(range(n)), 0, []
while p:
i = (i+k-1) % len(p)
seq.append(p.pop(i))
return 'Prisoner killing order: %s.\nSurvivor: %i' % (', '.join(str(i) for i in seq[:-1]), seq[-1])

>>> print(j(5, 2))
Prisoner killing order: 1, 3, 0, 4.
Survivor: 2
>>> print(j(41, 3))
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34, 15.
Survivor: 30
>>> </lang>


=={{header|REXX}}==
=={{header|REXX}}==
Line 219: Line 235:


Josephus is prisoner 31</pre>
Josephus is prisoner 31</pre>

=={{header|Python}}==
<lang python>>>> def j(n, k):
p, i, seq = list(range(n)), 0, []
while p:
i = (i+k-1) % len(p)
seq.append(p.pop(i))
return 'Prisoner killing order: %s.\nSurvivor: %i' % (', '.join(str(i) for i in seq[:-1]), seq[-1])

>>> print(j(5, 2))
Prisoner killing order: 1, 3, 0, 4.
Survivor: 2
>>> print(j(41, 3))
Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34, 15.
Survivor: 30
>>> </lang>

Revision as of 14:07, 16 November 2012

Josephus problem is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Josephus problem is a math puzzle with a grim description: prisoners are standing on a circle, sequentially numbered from to . An executioner walks along the circle, starting from prisoner , removing every -th prisoner and killing him. As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed. For example, if there are prisoners and , the order the prisoners are killed in (let's call it the "killing sequence") will be 1, 3, 0, and 4, and the survivor will be #2.

Task Given any , find out which prisoner will be the final survivor. In one such incident, there were 41 prisoners and every 3rd prisoner was being killed (). Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale. Which number was he?

Extra The captors may be especially kind and let survivors free, and Josephus might just have friends to save. Provide a way to calculate which prisoner is at any given position on the killing sequence.

Notes

  1. You can always play the executioner and follow the procedure exactly as described, walking around the circle, counting (and cutting off) heads along the way. This would yield the complete killing sequence and answer the above questions, with a complexity of probably . However, individually it takes no more than to find out which prisoner is the -th to die.
  2. If it's more convenient, you can number prisoners from to instead. If you choose to do so, please state it clearly.
  3. An alternative description has the people committing assisted suicide instead of being executed, and the last person simply walks away. These details are not relevant, at least not mathematically.


Go

<lang go>package main

import "fmt"

// basic task function func finalSurvivor(n, k int) int {

   // argument validation omitted
   circle := make([]int, n)
   for i := range circle {
       circle[i] = i
   }
   k--
   exPos := 0
   for len(circle) > 1 {
       exPos = (exPos + k) % len(circle)
       circle = append(circle[:exPos], circle[exPos+1:]...)
   }
   return circle[0]

}

// extra func position(n, k, pos int) int {

   // argument validation omitted
   circle := make([]int, n)
   for i := range circle {
       circle[i] = i
   }
   k--
   exPos := 0
   for len(circle) > 1 {
       exPos = (exPos + k) % len(circle)
       if pos == 0 {
           return circle[exPos]
       }
       pos--
       circle = append(circle[:exPos], circle[exPos+1:]...)
   }
   return circle[0]

}

func main() {

   // show basic task function on given test case
   fmt.Println(finalSurvivor(41, 3))
   // show extra function on all positions of given test case
   fmt.Println("Position  Prisoner")
   for i := 0; i < 41; i++ {
       fmt.Printf("%5d%10d\n", i, position(41, 3, i))
   }

}</lang>

Output:
30
Position  Prisoner
    0         2
    1         5
    2         8
    3        11
    4        14
    5        17
    6        20
    7        23
    8        26
    9        29
   10        32
   11        35
   12        38
   13         0
   14         4
   15         9
   16        13
   17        18
   18        22
   19        27
   20        31
   21        36
   22        40
   23         6
   24        12
   25        19
   26        25
   27        33
   28        39
   29         7
   30        16
   31        28
   32        37
   33        10
   34        24
   35         1
   36        21
   37         3
   38        34
   39        15
   40        30

Perl

Translation of: Perl6

<lang Perl>my @prisoner = 0 .. 40; my $k = 3; until (@prisoner == 1) {

   push @prisoner, shift @prisoner for 1 .. $k-1;
   shift @prisoner;

}

print "Prisoner @prisoner survived.\n"</lang>

Output:
Prisoner 30 survived.

Perl 6

Straightforward implementation of the executioner's algorithm: <lang Perl6>sub Execute(@prisoner is rw, $k) {

   until @prisoner == 1 {

@prisoner.=rotate($k - 1); @prisoner.shift;

   }

}

my @prisoner = ^41; Execute @prisoner, 3; say "Prisoner {@prisoner} survived.";</lang>

Output:
Prisoner 30 survived.

We don't have to use numbers. Any list will do:

<lang Perl6>my @dalton = <Joe Jack William Averell Rantanplan>; Execute @dalton, 2; say "{@dalton} survived.";</lang>

Output:
William survived.

Python

<lang python>>>> def j(n, k): p, i, seq = list(range(n)), 0, [] while p: i = (i+k-1) % len(p) seq.append(p.pop(i)) return 'Prisoner killing order: %s.\nSurvivor: %i' % (', '.join(str(i) for i in seq[:-1]), seq[-1])

>>> print(j(5, 2)) Prisoner killing order: 1, 3, 0, 4. Survivor: 2 >>> print(j(41, 3)) Prisoner killing order: 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 0, 4, 9, 13, 18, 22, 27, 31, 36, 40, 6, 12, 19, 25, 33, 39, 7, 16, 28, 37, 10, 24, 1, 21, 3, 34, 15. Survivor: 30 >>> </lang>

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=s /* until one alive prisoner */

 found=0                              /* start looking              */
 Do Until found=w                     /* until we have the third    */
   p=p+1                              /* next position              */
   If p=nn Then p=0                   /* wrap around                */
   If dead.p=0 Then                   /* a prisoner who is alive    */
     found=found+1                    /* increment found count      */
   End
 dead.p=1
 n=n-1                                /* shoot the one on this pos. */
 killed=killed p                      /* add to output              */
 End                                  /* End of main loop           */

Say 'killed:'subword(killed,1,20) /* output killing sequence */ Say ' 'subword(killed,21) /* output killing sequence */ Say 'Survivor(s):' /* show */ Do i=0 To 40 /* look for the surviving p's */

 If dead.i=0 Then Say i               /* found one                  */
 End</lang>

Output:

killed:2 5 8 11 14 17 20 23 26 29 32 35 38 0 4 9 13 18 22 27
       31 36 40 6 12 19 25 33 39 7 16 28 37 10 24 1 21 3 34 15
Prisoner 30 survives 

Scala

Executioner's Solution, not Josephus'

(Prisoners labeled 1 to n) <lang scala>def executed( prisonerCount:Int, step:Int ) = {

 val prisoners = ((1 to prisonerCount) map (_.toString)).toList
 def behead( dead:Seq[String], alive:Seq[String] )(countOff:Int) : (Seq[String], Seq[String]) = {
   val group = if( alive.size < countOff ) countOff - alive.size else countOff
   (dead ++ alive.take(group).drop(group-1), alive.drop(group) ++ alive.take(group-1))
 }
 def beheadN( dead:Seq[String], alive:Seq[String] ) : (Seq[String], Seq[String]) =
   behead(dead,alive)(step)
 def execute( t:(Seq[String], Seq[String]) ) : (Seq[String], Seq[String]) = t._2 match {
   case x :: Nil => (t._1, Seq(x))
   case x :: xs => execute(beheadN(t._1,t._2))
 }
 execute((List(),prisoners))

}

val (dead,alive) = executed(41,3)

println( "Prisoners executed in order:" ) print( dead.mkString(" ") )

println( "\n\nJosephus is prisoner " + alive(0) )</lang>

Output:
Prisoners executed in order:
3 6 9 12 15 18 21 24 27 30 33 36 39 1 5 10 14 19 23 28 32 37 41 7 13 20 26 34 40 8 17 29 38 11 25 2 22 4 35 16

Josephus is prisoner 31