# OK to post homework # Lucy Martinez, 04-10-22, Assignment 20 read `C20.txt`: with(combinat): #----------------------Problem 1-----------------------# # Write a procedure ConjLP(R) that inputs a finite set of positive integers R, and outputs the pair [M,C] # where M is a positive integer e 2 and C is a subset of {0,1,...,M-1} such that PR(n,R)[1]=0 if and only # if n mod M belongs to C. For example, ConjLP({1,2}) should output [3,{0}] # [Hint, first output LP(10000,R), call it, L, say, then look for the smallest M such that convert(L mod M,set) # has less than M elements. This is your conjectured M. Then, for that M, convert(L mod M,set) # is your conjectured C. ConjLP:= proc(R) local L,min,i, temp, j: L:=LP(10000,R); for i from 1 to 2000 do temp:=convert(L mod i,set); if nops(temp)1 then S:=S union {a}: fi: od: S: end: NimL(2,3); # {[0, 0], [1, 1], [2, 2], [3, 3]} #----------------------Problem 6-----------------------# # Consider a variant of 2-pile Nim where in addition to the Nim moves of removing any number of pennies # in one of the piles, a player is also allowed to remove the SAME number of pennies from both piles. # Write a procedure Nim2V(a,b) that inputs non-negative integers a and b and outputs 0,{} or 1,S # where the members of S are either of the form [i,0],[0,i],[i,i]. For example, Nim2V(2,1) should output 0,{} # Nim2V(3,2) should output 1,{[1,1]} Nim2V:= proc(a,b) local S,i,j,N,T: option remember: S:={}: N:=[]: N:=[op(N),a,b]: if a=0 and b=0 then RETURN(0,S): fi: for i from 1 to 2 do for j from 1 to N[i] do if j<=a and Nim2V(a-j,b)[1]=0 then S:=S union {[j,0]}: elif j<=b and Nim2V(a,b-j)[1]=0 then S:=S union {[0,j]}: elif j<=a and j<=b and Nim2V(a-j,b-j)[1]=0 then S:=S union {[j,j]}: fi: od: od: if S={} then 0,S: else 1,S: fi: end: Nim2V(3,2); # 1, {[1, 1], [2, 0]} Nim2V(4,3); # 1, {[2, 2]} Nim2V(2,1); # 0, {} #----------------------Problem 7-----------------------# # Write a procedure Nim2VL(N) that inputs a positive integer N and outputs the LIST of pairs [a,b], # 0 <= a <= b <= N, that are losing positions for the above game. with increasing order of the first component. Nim2VL:=proc(N) local S, L,All,a,i,j: option remember: S:={}: L:=[seq(seq(j$i,i=1..2),j=0..N)]: All:=permute(L,2): for a in All do if a[1]<=a[2] and Nim2V(a[1],a[2])[1]<>1 then S:=S union {a}: fi: od: S: end: