A generalized version of the Josephus problem would be to allow any skip
number, and any number of survivors. This would require three variables
input to the verb.
1. The number of people in the line (n)
2. The number skipped for each step (s)
3. The number of people left to go free at the end (r)
Given these three integers, what positions will be left at the end of the
process?
usage: s r jose n
where s = the number skipped, r = the number remaining, and n = the number
in line.
Here's an explicit function to solve the problem:
jose=: 4 : 0
f =. i.0 NB. Initialize output vector
s =. <:0{x NB. x(0) is the skip number
r =. 1{x NB. x(1) is the number left
I =. y NB. y is the total positions
L =. 1+i. y NB. L is the position list
whilst. I =. I-1 do.
L =. s |. L
f =. f, {. L
L =. }. L
end.
(r-y) {. f NB. Take the last r positions
)
Examples:
3 4 jose 20 NB. Skip 3, leave 4, out of 20
8 2 13 20
5 1 jose 200 NB. Skip 5, leave 1, out of 200
113
I have been trying to make this tacit, but no luck so far. I'm still pretty
much of a novice with tacit functionality.
Skip
--
Skip Cave
Cave Consulting LLC
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm