> #ok to post ; > #Yifan Zhang, 09/16/2020, hw4 ; > ; > #Q1. ; > CP:=proc(A,B) local a,b: > {seq(seq([a,b],a in A),b in B)}: > end: > CP({a,b,c},{b,c,d}); {[a, b], [a, c], [a, d], [b, b], [b, c], [b, d], [c, b], [c, c], [c, d]} ; > Mynops:=proc(A) local a: > > if not type(A,set) then > print(A, `is not a set `): > RETURN(FAIL): > fi: > > add(1, a in A): > end: > CheckMult:=proc(A,B) > evalb(Mynops(CP(A,B))=Mynops(A)*Mynops(B)): > end: > CheckMult({1,2},{3,4}); true ; > CheckAdd:=proc(A,B): > > > if not type(A,set) then > print(A, `is not a set `): > RETURN(FAIL): > fi: > > if not type(B,set) then > print(B, `is not a set `): > RETURN(FAIL): > fi: > > if A intersect B<>{} then > print(A,B, `have the following common elements `, A intersect B): > RETURN(FAIL): > fi: > > #This checks that if A and B are disjoint finite sets, then > #|A U B|= |A| + |B| > evalb(Mynops(A union B)=Mynops(A) + Mynops(B)): > end: > CheckAdd({1,2,3},{3,4,5}); {1, 2, 3}, {3, 4, 5}, `have the following common elements `, {3} FAIL ; > Words:=proc(A,n) local W1,w,a: > option remember: > if n=0 then > RETURN({[]}): > else > W1:=Words(A,n-1): > RETURN({seq(seq([op(w),a], a in A),w in W1)}): > fi: > end: > member([d,o,r,o,n],Words({d,o,n,r},5)); true ; > ; > ; > ; > #Q2. ; > #1 is the smallest positive integer. ; > ; > ; > ; > #Q3. ; > #U = {1, ..., 15} ; > #A1 = {3,6,9,12,15} => Comp(U,A1) = {1,2,4,5,7,8,10,11,13,14} ; > #A2 = {5,10,15}=> Comp(U,A2) = {1,2,3,4,6,7,8,9,11,12,13,14} ; > #LHS = Comp(U,A1) intersect Comp(U,A2) = {1,2,4,7,8,11,13,14}=> nops(Comp(U,A1)) = 8 ; > #nops(U) = 15; nops(A1) = 5; nops(A2) = 3; nops(A1 interset A2) = 1 ; > #RHS = 15-5-3+1 = 8 ; > #LHS = RHS. ; > ; > ; > #Maple Procedure. ; > PIE2:= proc(U,A1,A2) local A,B: > option remember: > > if not (type(A1,set) and type(A2,set) and type(U,set) )then > RETURN(1): > fi: > > if not (A1 subset U and A2 subset U)then > RETURN (2): > fi: > > A:=nops((U minus A1) intersect (U minus A2)); > B:=nops(U) - nops(A1) - nops(A2) + nops(A1 intersect A2); > [A,B]: > end: > ; > PIE2({1,2,3,4,5}, {1,2,3},{2,3,5}); [1, 1] ; > ; > ; > ; > #Q4. ; > PIE3:= proc(U,A1,A2,A3) local A,B: > option remember: > > if not (type (A1,set) or type(A2,set) or type(A3, set) or type(U, set)) then > RETURN (FAIL): > fi: > > if not (A1 subset U or A2 subset U or A3 subset U) then > RETURN (FAIL): > fi: > > A:=nops((U minus A1) intersect (U minus A2) intersect (U minus A3)): > B:=nops(U)- nops(A1)-nops(A2)-nops(A3) +nops(A1 intersect A2 ) + nops(A2 intersect A3) + nops(A1 intersect A3) - nops(A1 intersect A2 intersect A3): > [A,B]: > end: > ; > #For example: > U:={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};A1:={2,4,6,8,10,12,14};A2:={3,6,9,12,15};A3:={4,8,12}; Typesetting:-mprintslash([(U := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})],[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}]) Typesetting:-mprintslash([(A1 := {2, 4, 6, 8, 10, 12, 14})],[{2, 4, 6, 8, 10, 12, 14}]) Typesetting:-mprintslash([(A2 := {3, 6, 9, 12, 15})],[{3, 6, 9, 12, 15}]) Typesetting:-mprintslash([(A3 := {4, 8, 12})],[{4, 8, 12}]) ; > PIE3(U,A1,A2,A3); [5, 5] ; > #My maple procedure's answer is equal to the hand write answer. ; > ; > ; > ; > #Q5. ; > PIE4:= proc(U,A1,A2,A3,A4) local A,B: > option remember: > > if not (type(A1,set) or type(A2,set) or type(A3,set) or type(A4,set) or type(U,set)) then > RETURN(FAIL): > fi: > > if not (A1 subset U or A2 subset U or A3 subset U or A4 subset U) then > RETURN(FAIL): > fi: > > A:=nops((U minus A1) intersect (U minus A2) intersect (U minus A3) intersect (U minus A4)): > B:=nops(U) - nops(A1) - nops(A2) - nops(A3) - nops(A4) + nops(A1 intersect A2) + nops(A1 intersect A3) + nops(A1 intersect A4) + nops(A2 intersect A3 ) + nops(A2 intersect A4) + nops(A3 intersect A4) - nops(A1 intersect A2 intersect A3) -nops(A1 intersect A2 intersect A4) - nops(A2 intersect A3 intersect A4) + nops(A1 intersect A2 intersect A3 intersect A4): > [A,B]: > end: > ; > ; > U:= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; A1:={2,4,6,8,10,12,14}; A2:={3,6,9,12,15}; A3:={4,8,12}; A4:={5,10,15}; Typesetting:-mprintslash([(U := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})],[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}]) Typesetting:-mprintslash([(A1 := {2, 4, 6, 8, 10, 12, 14})],[{2, 4, 6, 8, 10, 12, 14}]) Typesetting:-mprintslash([(A2 := {3, 6, 9, 12, 15})],[{3, 6, 9, 12, 15}]) Typesetting:-mprintslash([(A3 := {4, 8, 12})],[{4, 8, 12}]) Typesetting:-mprintslash([(A4 := {5, 10, 15})],[{5, 10, 15}]) ; > PIE4(U,A1,A2,A3,A4); [4, 4] ; > ;