Nim is a game for 2 players, each alternating taking turns. Initially the players are given a position consisting of several piles, each pile having a finite number of tokens. On each turn a player picks a pile and then removes at least one token from that pile. The player who picks up the last token wins.
Assuming your opponent plays optimally, some positions cannot be won. Such positions are called "losing" (for the player whose turn it is to move next). The positions which are not losing are called winning. The following question has a mathematical answer:
QUESTION: Which positions are losing for the player whose turn it is to move?
The answer is determined by the "nim sum" of the piles. The "nim sum" of r piles with m1 elements in the 1st pile, m2 elements in the 2nd pile, ..., mr elements in the last (r-th) pile, is obtained as follows:
If each of the coordinates of the nim sum vector is is even then we call the position even. Otherwise, we call the position odd. In the book by Hardy and Wright referenced below, one finds that odd positions are winning and even position cannot be won if your opponent plays optimally.
If an initial position is not a losing position, what strategy should one use to win? Make a move which leaves you in an odd position.
The MAPLEV4 nim package has routines to play nim solitaire or nim with the computer. There is a help worksheet which may be downloaded as well. If you have MAPLEV4 and Windows 3.1 or better then here is how to load the nim package:
with(share): readshare(nim,games);
W.W.R. Ball and H.S.M. Coxeter, Mathematical recreation and essays, 13th ed, Dover, 1987
G. Hardy and E. Wright, An introduction to the theory of numbers, 5-th ed, Oxford Univ Press, 1979