blob: f30c1403673474c0256ce1c291d08ce659e06088 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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"
|