oPlus[a_,b_] := Union[Flatten[Outer[Plus,a,b]]] oPlusCheck[a_,b_] := Module[{op = oPlus[a,b]}, If [Length[a]*Length[a]== Length[op],op, {}]] firstGap[mySet_] := Module[{m=0}, While[MemberQ[mySet,m], If[m<=0,m=1-m,m=-m]] ;m] enlargeCanon[A_, B_]:= Module[{g=firstGap[oPlus[A,B]], m, Anew, Bnew, op}, (* declaring local variables *) m = firstGap[Union[A, g-B] ]; op = {}; (* now we try different m until it fits *) While[op=={}, Anew = Append[A,m]; Bnew = Append[B, g-m]; op = oPlusCheck[Anew, Bnew]; ; m++ ]; (* should be OK by now, we list the solutions after sorting them *) Sort /@ {Anew, Bnew } ] |