Hiya... I've derived a general solution for the Josephus Flavius problem, but it's a bit awkward to express in conventional mathematical notation. I'd be interested to see if anyone can convert this to a closed form.In Python:
def flavius(n, p):
if n == 1: return 0
if n < p: return (flavius(n - 1, p) + p) % n
q, r = n/p, n%p
i = flavius(n - q, p) - r
if i < 0: return i + n
return i + i / (p - 1)
Or as a piecewise function:
f(n, p) = { 0, n = 1
( f(n-1, p) + p (mod n), n < p
( f(n-q, p) - r + n, f(n-q, p) < r
( f(n-q, p) x (p/) otherwise
where q is the quotient (n-r)/p and r is the remainder n (mod p).
Note that this function returns the zero-based index of the survivor.