about | help | code help+videos | done | prefs |
nim
In the game of Nim, there is a pile of cigars (or they could be squirrels). You and another player take turns removing cigars from the pile. On each turn you can take 1, 2, or 3. The player to take the last cigar wins. Write the function nim(n), which returns the number of the n cigars you should take to guarantee an eventual win regardless of what the other player does. If there is no way to guarantee a win, just take 1 cigar -- leaving as big a pile as possible so maybe the opponent will make a mistake. nim(3) → 3 nim(7) → 3 nim(8) → 1 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 300 Post-solution available
Copyright Nick Parlante 2017 - privacy