Skip to content

Instantly share code, notes, and snippets.

@junminstorage
Last active August 5, 2020 15:46
Show Gist options
  • Save junminstorage/92ba54521940a2ff9c03b4cf3c23d946 to your computer and use it in GitHub Desktop.
Save junminstorage/92ba54521940a2ff9c03b4cf3c23d946 to your computer and use it in GitHub Desktop.
Let's say we label each person starting with index 0, for N persons, we have
0, 1, 2, 3, ...., N-1.
Fn(N, K) return the index of the last person, with starting position at 0
Given N persons, after removing the Kth person, then the next starting position will be K%N
Fn(N-1, K) return the position of the survivor with starting position at 0 then for starting position of K%N
(Fn(N-1, K) + K)%N will return the position of the survivor.
so Fn(N, K) = (Fn(N-1, K) + K)%N
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment