The Problem

Imagine you have a circle of people and you go around the circle removing every second person until one person is left.

If you have 3 people in the circle, then the 3rd person will be the last one remaining. If you have 4 people then the 1st person will be the last one remaining. If you have 11 people then the 7th person will be the one remaining.

If you have N people in the circle, who will be the last one remaining?

Please formulate your answer as a general solution and not as an algorithm that simulates the problem

Solution

The function is basic this f(n,k). the variable n represents the number of peoples and k represents the skips. So, the solution is basically a recursive solution

f(n,k) = ( k-1 + f(n-1,k)) %n + 1 f(1,k) = 1

The value k -1 and the added value of f(n-1, k) just to set the circle in position k -1 , remembering what f ( n-1, k ) returns the first position Relationship with and the value+1 and to properly adjust the position k . The amount of time you should skip to the last person is defined by the number of people.

Share this project:

Updates