Two players A and B playing a game both players play alternately and optimally starting from A. In each turn, a player can remove 1 $\leq$ cards $\leq$ k cards from the pile. Player moving the last move win the game. Find the winner. Here 1 $\leq$ k $\leq$ n.
In this question i tried my approach by finding Mex function for all Grundy numbers then by taking XOR of all Grundy numbers if XOR of all numbers is not equal to zero then Player A wins the game otherwise Player B wins.
If $k=3$ then you correctly computed $G(1)=1$, $G(2)=2$, and $G(3)=3$, but $$G(4)=\text{mex}(\{G(1),G(2),G(3)\})=\text{mex}(\{1,2,3\})=0.$$
Hint. Show that for a given $k$, the Grundy function $G$ is periodic of period $k+1$. Note that $G(0)=0$.
Can you take it from here?