0||0|106|0| 0|0|0|||||Josephus Flavius - a generalized solution|David Turner|1|08:17:15|05/14/2008|Hiya... I%27ve derived a general solution for the Josephus Flavius problem%2C but it%27s a bit awkward to express in conventional mathematical notation. I%27d be interested to see if anyone can convert this to a closed form.%0D%0A%0D%0AIn Python%3A%0D%0A%0D%0A%5Bpre%5D%0D%0Adef flavius%28n%2C p%29%3A%0D%0A if n %3D%3D 1%3A return 0%0D%0A if n %3C p%3A return %28flavius%28n - 1%2C p%29 %2B p%29 %25 n%0D%0A q%2C r %3D n%2Fp%2C n%25p%0D%0A i %3D flavius%28n - q%2C p%29 - r%0D%0A if i %3C 0%3A return i %2B n%0D%0A return i %2B i %2F %28p - 1%29%0D%0A%5B%2Fpre%5D%0D%0A%0D%0AOr as a piecewise function%3A%0D%0A%0D%0A%5Bpre%5D%0D%0Af%28n%2C p%29 %3D %7B 0%2C n %3D 1%0D%0A %28 f%28n-1%2C p%29 %2B p %28mod n%29%2C n %3C p%0D%0A %28 f%28n-q%2C p%29 - r %2B n%2C f%28n-q%2C p%29 %3C r%0D%0A %28 f%28n-q%2C p%29 x %28p%2F%5Bp-1%5D%29 otherwise%0D%0A%5B%2Fpre%5D%0D%0A%0D%0Awhere q is the quotient %28n-r%29%2Fp and r is the remainder n %28mod p%29.%0D%0A%0D%0ANote that this function returns the zero-based index of the survivor.