Nim is a solved game. The winning strategy revolves around the nim-sum, which is the bitwise XOR of all pile sizes.
The key insight: A position is losing (for the player about to move) if and only if the nim-sum is zero. In standard (last-takes-wins) play, you want to leave your opponent with a nim-sum of 0.
How to compute: Write each pile size in binary, then XOR them together. For example, piles of (3, 5, 7):
3 = 011
5 = 101
7 = 111
XOR = 001 (nim-sum = 1)
Since the nim-sum is non-zero, this is a winning position for the player to move. They should remove stones to make the nim-sum zero.
Misere variation: Play optimally as in normal Nim, but when all piles are 0 or 1, take so that an odd number of piles remain (forcing your opponent to take the last one).
Single pile (take 1-3): Leave a multiple of 4 stones for your opponent. Whatever they take (1, 2, or 3), you take the complement to maintain multiples of 4.
Nim was mathematically solved by Charles L. Bouton at Harvard in 1901. It's the foundation of combinatorial game theory, which Sprague and Grundy later generalized to all impartial games.