Problem: You have 100 balls with their numbers written on them. You throw away the first ball, skip the second, throw away the third, skip the fourth, etc. When you get to the end you loop back to the beginning of the remaining balls. So after the first pass through you'll end up with the even numbered balls (2, 4, 6, ... 100). Because on the first pass you kept 100, on the second pass through you'll throw away 2, keep 4, throw away 6, etc. You keep throwing away a ball and skipping one until there's one ball left. Which numbered ball is left?
I know the answer by just going through it manually, but I was wondering if there's a way to do this with a formula/Algorithm/some math way. I'll be translating the answer into computer code.
Thank you.