Mr.S and Mr. P puzzle — "Formalization of two Puzzles Involving Knowledge", McCarthy, John (1987)
We pick two numbers $a$ and $b$, such that $a\geq b$ and both numbers are within the range $(2,99)$. We give Mr.P the product $a b$ and give Mr.S the sum $a+b$. Then following dialog takes place:
Mr.P: I don't know the numbers
Mr.S: I knew you didn't know. I don'tknow either.
Mr.P: Now I know the numbers
Mr.S: Now I know them tooCan we find the numbers $a$ and $b$?
I tried to do this, but it is very slow. I am sure there must be a simpler way.
Clear[pool, f1, f2, f3];
pool = Join @@ Table[{i, j}, {i, 2, 99}, {j, 2, i}];
f1[x_] := Length@Select[pool, Times @@ # == x &] != 1
f2[x_] := Length@Select[pool, Plus @@ # == x &] != 1
f3[x_] := And @@ (f1 /@ (Times @@@ Select[pool, Plus @@ # == x &]))
f4[x_] := Length@Select[Select[pool, Times @@ # == x &], f3[#[[1]] + #[[2]]] &] == 1
f5[x_] := Length@Select[Select[pool, Plus @@ # == x &], f4[#[[1]]*#[[2]]] &] == 1
Select[pool,
f1[#[[1]]*#[[2]]] && f2[#[[1]] + #[[2]]] && f3[#[[1]] + #[[2]]] &&
f4[#[[1]]*#[[2]]] && f5[#[[1]] + #[[2]]] &] // Timing
Answer
I tried to understand the other two solutions, but honestly, I couldn't. So I tried to write a version that is easier to understand.
EDIT: I've refactored the code a little, primarily pulling out the "knowledge operators" personKnowsSolution
and personKnowsProperty
. I'm not really following McCarthy's axiomatization of knowledge, this is just my ad-hoc way of expressing "knowledge" in Mathematica.
Clear[personKnowsSolution, personKnowsProperty, mrP, mrS]
personKnowsSolution[informationFilter_, possibilities_] :=
Join @@ Select[GatherBy[possibilities, informationFilter], Length[#] == 1 &]
personKnowsProperty[informationFilter_, possibilities_, property_] :=
Select[possibilities, property[informationFilter[#]] &]
mrP[{a_, b_}] := a*b
mrS[{a_, b_}] := a + b
(
allPossibilities = Join @@ Table[{i, j}, {i, 2, 99}, {j, 2, i}];
(* Mr. P doesn't know the solution *)
mrPWouldKnowSolution = personKnowsSolution[mrP, allPossibilities];
mrPDoesntKnowSolution = Complement[allPossibilities, mrPWouldKnowSolution];
(* Mr. S doesn't know the solution *)
mrSWouldKnowSolution = personKnowsSolution[mrS, allPossibilities];
mrSDoesntKnowSolution = Complement[allPossibilities, mrSWouldKnowSolution];
(* Mr. S knows Mr. P doesn't know the solution *)
sumsWhereMrPWouldKnowTheSolution = Union[mrS /@ mrPWouldKnowSolution];
mrSKnowsMrPDoesntKnow =
personKnowsProperty[mrS, mrSDoesntKnowSolution,
Not[MemberQ[sumsWhereMrPWouldKnowTheSolution, #]] &];
(* Given that, Mr. P knows the solution *)
mrPKnowsTheSolution = personKnowsSolution[mrP, mrSKnowsMrPDoesntKnow];
(* Given that, Mr. S knows the solution *)
mrSKnowsTheSolution = personKnowsSolution[mrS, mrPKnowsTheSolution]
) // Timing
Output: {0.063, {{13, 4}}}
Comments
Post a Comment