#OK to post homework #Karnaa Mistry, 09/20/20, Assignment HW #4 with(combinat): # 1. # 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]} # CheckMult({1,2},{3,4}) = true # CheckAdd appears to reference proc CheckDecomp, so: # CheckDecomp({1,2,3},{3,4,5}) = {1, 2, 3}, {3, 4, 5}, `have the following common elements `, {3} FAIL # member([d,o,r,o,n],Words({d,o,n,r},5)) = true # 2. I found that n = 106 # 3. # Checking PIE by hand: U = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; A1 = {3,6,9,12,15}; A2 = {5,10,15}; # Comp(U,A1) = {1,2,4,5,7,8,10,11,13,14}; Comp(U,A2) = {1,2,3,4,6,7,8,9,11,12,13,14}; # A1 intersect A2 = {15}; # Comp(U,A1) intersect Comp(U,A2) = {1,2,4,7,8,11,13,14}; # |Comp(U,A1) intersect Comp(U,A2)| = 8 # |U| = 15; |A1| = 5; |A2| = 3; |A1 intersect A2| = 1 # Does 8 = 15 - 5 - 3 + 1? # Yes, 8 = 8, and so PIE for these sets are verified #PIE2(U,A1,A2): that inputs three sets U, A1, A2. After checking that U, A1,A2, are indeed sets (returns FAIL if not) #AND that A1 and A2 are subsets of U (returns FAIL otherwise), computes PIE in two ways. The first way by directly #finding |Comp(U,A1) intersect Comp(U,A2)| and the second way by using the formula PIE2:=proc(U,A1,A2) local i,A1comp,A2comp: option remember: if (not type(U,set)) or (not type(A1,set)) or (not type(A2,set)) then RETURN(FAIL): fi: if (not A1 subset U) or (not A2 subset U) then RETURN(FAIL): fi: A1comp:={}: A2comp:={}: for i from 1 to numelems(U) do if not (U[i] in A1) then A1comp:=A1comp union {U[i]}: fi: od: for i from 1 to numelems(U) do if not (U[i] in A2) then A2comp:=A2comp union {U[i]}: fi: od: RETURN([numelems(A1comp intersect A2comp), numelems(U)-numelems(A1)-numelems(A2)+numelems(A1 intersect A2)]): end: # 4. optional PIE3(U,A1,A2,A3) #PIE3(U,A1,A2,A3): similar to PIE2 but for 3 sets, using an appropriate PIE formula to prevent overcounting PIE3:=proc(U,A1,A2,A3) local i,A1comp,A2comp,A3comp: option remember: if (not type(U,set)) or (not type(A1,set)) or (not type(A2,set)) or (not type(A3,set)) then RETURN(FAIL): fi: if (not A1 subset U) or (not A2 subset U) or (not A3 subset U) then RETURN(FAIL): fi: A1comp:={}: A2comp:={}: A3comp:={}: for i from 1 to numelems(U) do if not (U[i] in A1) then A1comp:=A1comp union {U[i]}: fi: od: for i from 1 to numelems(U) do if not (U[i] in A2) then A2comp:=A2comp union {U[i]}: fi: od: for i from 1 to numelems(U) do if not (U[i] in A3) then A3comp:=A3comp union {U[i]}: fi: od: RETURN([numelems(A1comp intersect A2comp intersect A3comp), numelems(U)-numelems(A1)-numelems(A2) -numelems(A3)+numelems(A1 intersect A2)+numelems(A2 intersect A3)+numelems(A3 intersect A1) -numelems(A1 intersect A2 intersect A3)]): end: # 5. optional PIE4(U,A1,A2,A3,A4) #PIE4(U,A1,A2,A3,A4): similar to PIE2 but for 4 sets, using an appropriate PIE formula to prevent overcounting PIE4:=proc(U,A1,A2,A3,A4) local i,A1comp,A2comp,A3comp,A4comp: option remember: if (not type(U,set)) or (not type(A1,set)) or (not type(A2,set)) or (not type(A3,set)) or (not type(A4,set)) then RETURN(FAIL): fi: if (not A1 subset U) or (not A2 subset U) or (not A3 subset U) or (not A4 subset U) then RETURN(FAIL): fi: A1comp:={}: A2comp:={}: A3comp:={}: A4comp:={}: for i from 1 to numelems(U) do if not (U[i] in A1) then A1comp:=A1comp union {U[i]}: fi: od: for i from 1 to numelems(U) do if not (U[i] in A2) then A2comp:=A2comp union {U[i]}: fi: od: for i from 1 to numelems(U) do if not (U[i] in A3) then A3comp:=A3comp union {U[i]}: fi: od: for i from 1 to numelems(U) do if not (U[i] in A4) then A4comp:=A4comp union {U[i]}: fi: od: RETURN([numelems(A1comp intersect A2comp intersect A3comp intersect A4comp), numelems(U) -numelems(A1)-numelems(A2)-numelems(A3)-numelems(A4)+numelems(A1 intersect A2) +numelems(A2 intersect A3)+numelems(A3 intersect A4)+numelems(A4 intersect A1) +numelems(A1 intersect A3)+numelems(A2 intersect A4) -numelems(A1 intersect A2 intersect A3)-numelems(A1 intersect A3 intersect A4) -numelems(A2 intersect A3 intersect A4)-numelems(A1 intersect A2 intersect A4) +numelems(A1 intersect A2 intersect A3 intersect A4)]): end: