Tuesday, October 2, 2012

F# on Algorithms - Josephus Problem

the description Josephus problem is listed here. The algorithm below listed the one survive at last and his number in the initial circle.


let rec josephusProblem n k =
    match n with
    | 1 -> 1
    | _ -> ((josephusProblem (n-1) k) + k - 1 ) % n + 1

josephusProblem 14 2

No comments: