Random Josephus problem Josephus and 40 soldiers are playing a Game of Death.
At the beginning, no.1 has a gun, he can kill the person on his left one (no.41  50%) or the person on his right (no.2  50%).
Then he gives the gun to the next person on his right who is still alive.
All subsequent agents face the same choice, until only the last one stands.

Which side should Josephus start to have a higher survival probability?

 A: The question of determining the probabilities of each of them to survive can be solved for arbitrary $n$ via recursion. We know, that if Josephus was alone, he would have been the survivor with probability $1$. If there are $n$ soldiers and one of them with a gun, then after he makes a shot we will have two similar tasks (for two outcomes) with $n - 1$ soldiers (but those soldiers in each of the cases will be different). This recursive method was implemented in the following Python code that solves the problem for $n$ soldiers ($n$ can be chosen manually):
import numpy as np
from fractions import Fraction

def rnext(l, a):
    for i in range(1, len(l)):
        if l[(a + i)%len(l)] == 1:
            return (a + i)%len(l)
    return a

def lnext(l, a):
    for i in range(1, len(l)):
        if l[(a - i)%len(l)] == 1:
            return (a - i)%len(l)
    return a

def singleton(n, a):
    l = np.asarray(list([0 for i in range(n)]))
    l[a] += 1
    return l

def josephus(l, a):
    if rnext(l, a) == a:
        return np.asarray(list([Fraction(i, 1) for i in list(singleton(len(l), a))]))
    else:
        return Fraction(1, 2)*(josephus(l - singleton(len(l), lnext(l, a)), rnext(l - singleton(len(l), lnext(l, a)), a)) + josephus(l - singleton(len(l), rnext(l, a)), rnext(l - singleton(len(l), rnext(l, a)), a)))

n = int(input())
l = np.ones(n)
j = josephus(l, 0)
for i in range(n):
    print(i, j[i])

