Help := proc() if args = NULL then print(`Mex,NS,Ka,SG,TAG `); print(`DC,TBG,GG,PrimPlus`); print(``); print(`Followed Winning Ways chapter 4`); print(`Mex = minimal excluded number`); print(`NS = Nim Sum, Ka = Kayles`); print(`SG = Subtraction games, TAG = Take-away games`); print(`DC = Dawson's Chess, TBG = Take-and-break games`); print(`GG = Grundy's game`); print(`PrimPlus = Prim plus in Prim and Dim`); elif args = `Mex` then print(`Mex(S), input the set of integer S`); print(`return the smallest nonnegative integer that is not in S`); elif args = `NS` then print(`NS(a,b), input a,b the number of coin in heap1 and heap2`); print(`return the nim-value of this game`); elif args = `Ka` then print(`Ka(n), input the positive interger n`); print(`Left and Right take turn to remove one`); print(`or two consecutive pins`); print(`Last player who does not have a move lost`); print(`return the nim-values of this game`); elif args = `SG` then print(`SG(n,S), input a number of bean n and `); print(`a subtraction set S`); print(`Left and Right take turn to remove the bean`); print(`Last player who does not have a move lost`); print(`return the nim-values of this game`); elif args = `TAG` then print(`TAG(n), input the heap of size n`); print(`the rule is remove a heap containing just one bean`); print(`or remove two beans from any heap with more than two`); print(`or remove three beans from any heap`); print(`Last player who does not have a move lost`); print(`return the nim-values of this game`); elif args = `DC` then print(`DC(n), input the 3 by n chessboard`); print(`n white pawn on row 1 , n black pawn on row 3`); print(`Left and Right take turn to move the pawn`); print(`if he can take, he have to take`); print(`Last player who does not have a move lost`); print(`return the nim-values of this game`); elif args = `TBG` then print(`TBG`); print(`The most generalize game of this chapter`); print(`have not programmed it yet`); print(``); elif args = `GG` then print(`GG(n), input the heap of size n`); print(`legal move is to split a single heap into`); print(`two smaller ones of different size`); print(`Last player who does not have a move lost`); print(`return the nim-values of this game`); elif args = `PrimPlus` then print(`PrimPlus(n), input the heap of size n`); print(`you can remove m beans from a heap of size n `); print(`provided that m and n are coprime`); print(`1 to 0 is legal`); print(`Last player who does not have a move lost`); print(`return the nim-values of this game`); elif args = `` then print(``); print(``); print(``); print(``); else print(`there is no help for`, args); fi: end: ########################################################### Mex := proc(S) local A,i: A := sort([op(S)]); if A[1] < 0 then A := [op({op(A)} minus {seq(i,i = A[1]..-1)})]; fi: for i from 0 to nops(A)-1 do if A[i+1] <> i then return(i): fi: od: return(nops(A)); end: NS := proc(a,b) option remember: local i,x,y,sol: x := convert(a,base,2): y := convert(b,base,2): x := [op(x),0$(nops(y)-nops(x))]; y := [op(y),0$(nops(x)-nops(y))]; return(add( 2^(i-1)*(x[i]+y[i] mod 2), i = 1..nops(x))); end: Ka := proc(n) option remember: if n = 0 then return(0); fi; if n = 1 then return(1); fi; return(Mex( {seq(NS(Ka(n-i),Ka(i-1)),i=1..n) ,seq(NS(Ka(n-i),Ka(i-2)),i=2..n)})); end: SG := proc(n,S) option remember: if n < 0 then return(); fi: if n < min(op(S)) and n >= 0 then return(0); fi; return(Mex({seq(SG(n-S[i],S),i=1..nops(S))})); end: TAG := proc(n) option remember: if n < 0 then return(); fi: if n = 0 or n = 2 then return(0); fi: if n = 1 then return(1); fi: return(Mex({TAG(n-2),TAG(n-3)})); end: DC := proc(n) option remember: if n < -1 then return(); fi: if n = 0 or n = -1 then return(0); fi: return(Mex({seq(NS(DC(i),DC(n-i-3)),i=-1..n-2)})); end: TBG := proc(n,S) option remember: end: GG := proc(n) option remember: if n = 1 or n=2 then return(0); fi; return(Mex({seq(NS(GG(i),GG(n-i)),i=1..ceil(n/2)-1)})) ; end: PrimPlus := proc(n) local i,a,A; option remember: if n < 0 then return(); fi; if n = 0 then return(0); fi; if n = 1 then return(1); fi; A := {}; for i from 1 to n-1 do if gcd(i,n) = 1 then A := A union {i}; fi: od: return(Mex({seq(PrimPlus(n-a),a in A)})); end: