#Maple routines to solve the "Can you combine a,b,c,d to make e?" questions #We define procedures sol() fourfours() and twentyfour() #For details: http://www.math.niu.edu/~rusin/uses-math/games/krypto/ BB7:=[x1/(x2/x3+x4), x1/(x2/x3-x4), x1/(x4-x2/x3)]: BB6:=[x1/(x2-x3-x4), x1/(x2+x3+x4), x1/(x2-x3+x4), x1/(x2-x3*x4), x1/(x2*x3+x4), x1/(x2*x3-x4), x1/(x2+x3)/x4, x1/(x2-x3)/x4]: BB5:=[x1/(x2-x3)*x4, (x1+x4)/(x2-x3), (x1-x4)/(x2-x3), x1/(x2-x3)+x4, x1/(x2-x3)-x4]: BB4:=[(x1+x2)/(x3+x4), (x1-x2)/(x3+x4), x1*x2/(x3+x4), x1+x2/(x3+x4), x1-x2/(x3+x4), x2/(x3+x4) - x1]: BB3:=[x1/x2/x3/x4]: BB2:=[x1*x2/x3/x4, (x1+x2)/x3/x4, (x1-x2)/x3/x4, x1/x3+x2/x4, x1/x3-x2/x4, (x2-x1/x3)/x4, (x1/x3+x2)/x4, (x1/x3-x2)/x4, x1/x3/x4+x2, x1/x3/x4-x2, x2-x1/x3/x4]: BB1:=[(x1+x2+x3)/x4, (x1+x2-x3)/x4, (x1-x2-x3)/x4, x1*x2*x3/x4, x1*(x2-x3)/x4, x1*(x2+x3)/x4, (x1*x2-x3)/x4, (x1*x2+x3)/x4, (x1-x2*x3)/x4, (x1-x2)/x4-x3, (x1-x2)/x4+x3, (x1+x2)/x4-x3, (x1+x2)/x4+x3, x3-(x1+x2)/x4, x1*x2-x3/x4, x1*x2+x3/x4, x3/x4-x1*x2, x1/x4-x2*x3, x1/x4+x2*x3, x2*x3-x1/x4, x3*x1/x4-x2, x3*x1/x4+x2, x2-x3*x1/x4, x1*(x2+x3/x4), x1*(x2-x3/x4), x1*(x3/x4 - x2), x1+x2+(x3/x4), x1+x2-(x3/x4), (x3/x4)-x1-x2, x1-x2+(x3/x4), x1-x2-(x3/x4)]: BB0:=[x1*x2*x3*x4, x1+x2+x3+x4, x1+x2+x3-x4, x1+x2-x3-x4, x1-x2-x3-x4, x1*x2*(x4-x3), x1*x2*(x3+x4), x1*x2+x3+x4, x1*x2+x3-x4, x1*x2-x3-x4, x3-x4-x1*x2, x3+x4-x1*x2, x1+x2*x3*x4, x1-x2*x3*x4, x2*x3*x4-x1, (x1+x2+x3)*x4, (x1+x2-x3)*x4, (x1-x2-x3)*x4, (x1*x2+x3)*x4, (x1*x2-x3)*x4, (x3-x1*x2)*x4, (x1+x2)*(x3+x4), (x1-x2)*(x3+x4), (x1-x2)*(x3-x4), x1*x2+x3*x4, x1*x2-x3*x4, x1*(x2+x3)+x4, x1*(x2-x3)+x4, x4-(x2+x3)*x1, x1*(x2+x3)+x4, x1*(x2+x3)-x4, x1*(x2-x3)-x4]: BB:=[seq(op(BB||k),k=0..7)]: #I get 97 formulas. print(nops(BB)); with(combinat): T:=permute([t1,t2,t3,t4]): ALL:=[op({seq(op(factor(subs({seq(x||k=T[i][k],k=1..4)},BB))),i=1..24)})]: #Looks like 1170 possible values. print(nops(ALL)); sol:=proc(a,b,c,d,e) local T,t,bb,nn,dd: T:=permute([a,b,c,d]): for t in T do for bb in BB do bx:=factor(bb): nn:=subs({x1=t[1],x2=t[2],x3=t[3],x4=t[4]},numer(bx)): dd:=subs({x1=t[1],x2=t[2],x3=t[3],x4=t[4]},denom(bx)): if dd <> 0 then if nn/dd=e then lprint(t,bb,nn/dd):fi:fi: od:od: end: #Example giving 1170 distinct values: T := [17, 25, 29, 31]: #From these four numbers, one can form -- uniquely -- the positive integers #2, 4, 5, 6, 9, 10, 12, 16, # 18, 21, 33, 37, 40, 44, 52, 56, 59, 67, 68, 72, 73, 84, 99, 102, ... #and their negatives EXCEPT -5, -102, ... (5=(25+29+31)/17, 102=sum!) #Sample attempt to solve a hand: 17, 25, 29, 31 -> 2 #Use like this: sol(17,25,29,31,2); ##Note that apparently-multiple solutions are redundant: #[17, 25, 31, 29] (x1-x4)/(x2-x3) 2 #[29, 31, 25, 17] (x1-x4)/(x2-x3) 2 ##This reflects the stabilizer group of the particular formula:<(14)(23)>. #This also 'solves' the four fours game: We can evaluate all #but six of the 97 elements of BB with x1=x2=x3=x4 = 4 and get #these 56 combinations ONLY: # #[-60, -48, -28, -16, -15, -12, -8, -7, -4, -15/4, -7/2, -3, -2, -4/3, -1, # -3/4, -1/3, 0, 1/16, 1/8, 1/5, 1/3, 1/2, 3/4, 4/5, 1, 5/4, 4/3, 2, 3, 7/2, # 15/4, 4, 17/4, 9/2, 5, 6, 7, 8, 9, 12, 15, 16, 17, 20, 24, 28, 32, 36, 48, # 60, 64, 68, 80, 128, 256] # #In particular: no 10,11,13,14,18,19,etc. fourfours:=proc(n) sol(4,4,4,4,n); end: # We can also use this for the game '24'; e.g. I was asked about [10,10,4,4]; # sol(10,10,4,4,24); # [10, 10, 4, 4] (x1*x2-x3)/x4 24 twentyfour:=proc(a,b,c,d) sol(a,b,c,d,24); end: