The issue is that you are using an int which is 16bit. That means that it's not 1010 but 0000000000001010. Your shift makes it x101000 which is 32+8 =40.
You need to create your own object that is [nop] long and rolls over. I suggest using a bool array and set it to true when the index is killed. You can shrink the circle by one by using linq to return only the people where the array item == false. Each time the counter rolls on you can use mod (%) to count on with a rollover.
Array.where(p=>!p).toarray()[counter%length]=true.
I am working on some sample code to show you but ran out of time. I wanted to give you advanced notice of my answer. I will post tomorrow (9:30 gmt)
EDIT:
Ok - I think this works.
There is another variable you can use. There is the total number of people, the starting position AND the number to move on by. Currently the example uses the same number for the starting position and the number to move on by, but you can adjust it fairly easily.
It's not the most efficient but I wanted to break it down so it was obvious what was going on. The Index set is where all the rollover happens so I have put over 5 lines what could be done on one.
The array is obviously zero indexed so I have adjusted the input and final output by 1 so you can use 'real case' numbers.
If this isn't correct then it's pretty damn close.
class Program
{
class DeathCircle
{
private int _moveByNumber;
public DeathCircle(int peopleCount, int startingPosition)
: this(peopleCount, startingPosition, startingPosition)
{
}
public DeathCircle(int peopleCount, int startingPosition, int moveByNumber)
{
_people = new bool[peopleCount];
Index = startingPosition-1;
_moveByNumber = moveByNumber;
}
private readonly bool[] _people;
private int _index;
private int Index
{
get { return _index; }
set
{
int indexOfAlive = (value) % PeopleLeftAlive;
var setOfAllPeopleWithIndex = _people.Select((p, index) => new { p, index });
var setOfPeopleAliveWithIndex = setOfAllPeopleWithIndex.Where(n => !n.p);
var theNextVictim = setOfPeopleAliveWithIndex.Skip(indexOfAlive).First();
_index = theNextVictim.index;
}
}
public void Move()
{
Index += _moveByNumber;
}
public void Execute()
{
_people[Index] = true;
}
public int PeopleLeftAlive
{
get
{
return _people.Where(p => !p).Count();
}
}
public int Survivor
{
get
{
return _people.Select((p, i) => new { p, i }).First(n => !n.p).i + 1;
}
}
}
static void Main(string[] args)
{
int nop, bp;
string s;
Console.WriteLine("Enter the number of persons:");
s = Console.ReadLine();
nop = Convert.ToInt32(s);
Console.WriteLine("Enter the first person:");
s = Console.ReadLine();
bp = Convert.ToInt32(s);
var dc = new DeathCircle(nop, bp);
while (dc.PeopleLeftAlive > 1)
{
dc.Move();
dc.Execute();
}
Console.WriteLine(dc.Survivor);
Console.ReadLine();
}
}
I'd give me 5 stars ^_^
Enjoy