open System let rec josephus = function | 1 -> 1 | x -> (if x % 2 <> 0 then 1 else -1) + 2 * josephus (x / 2) let josephus' (target: int): int = let pow = Math.Pow(3, Math.Floor(Math.Log(target, 3))) |> int if pow = target then pow else if pow >= target / 2 then target - pow else pow + (2 * (target - 2 * pow)) josephus 3017957 |> printfn "%d" josephus' 3017957 |> printfn "%d"