#OK to post #Homework 20, Rebecca Embar read `C20.txt`: #1 ConjLP := proc(R) local L,M,C: L := LP(10000,R): for M from 1 to 10000 do C := convert(L mod M, set): if nops(C) < M then RETURN([M,C]): fi: od: RETURN(fail): end: #3 Nim := proc(N) local S,n,i,j: S := {}: for n from 1 to nops(N) do if N[n] > 0 then for i from 1 to N[n] do if Nim([seq(N[j],j=1..n-1),N[n]-i,seq(N[j],j=n+1..nops(N))])[1] = 0 then S := S union {[n,i]}: fi: od: fi: od: if S = {} then 0,S: else 1,S: fi: end: #4 with(combinat,cartprod): NimL := proc(k,N) local P,i,j,LP,p: P := cartprod([seq({seq(i,i=0..N)},j=1..k)]): LP := {}: while not P[finished] do p := P[nextvalue](): if Nim(p)[1] = 0 then LP := LP union {p}: fi: od: LP: end: #5 #NimL3: outputs set of weakly increasing lists of length 3 #with entries <= N that are losing positions for 3-pile Nim #MUCH faster than NimL NimL3 := proc(N) local P,a,b,LP,WP,p,j,i,k: if N=0 then return({[0,0,0]}): fi: P := {seq(seq([a,b,N],b=a..N),a=0..N)}: LP := NimL3(N-1): for p in P do for j from 1 to 3 do if p[j] > 0 then for i from 1 to p[j] do if sort([seq(p[k],k=1..j-1),p[j]-i,seq(p[k],k=j+1..3)]) in LP then next 3: fi: od: fi: od: LP := LP union {p}: od: LP: end: #6 Nim2V := proc(a,b) local S,i: S := {}: if a > 0 then for i from 1 to a do if Nim2V(a-i,b)[1] = 0 then S := S union {[i,0]}: fi: od: if b > 0 then for i from 1 to min(a,b) do if Nim2V(a-i,b-i)[1] = 0 then S := S union {[i,i]}: fi: od: fi: fi: if b > 0 then for i from 1 to b do if Nim2V(a,b-i)[1] = 0 then S := S union {[0,i]}: fi: od: fi: if S = {} then 0,S: else 1,S: fi: end: #7 Nim2VL := proc(N) local P,a,b,LP,p: P := {seq(seq([a,b],b=a..N),a=0..N)}: LP := {}: for p in P do if Nim2V(p[1],p[2])[1] = 0 then LP := LP union {p}: fi: od: LP: end: