/* Squares https://puzzles9.com/puzzles/number-sequence-puzzles-with-answers/ Puz1059 """ 13 5 12 4 5 20 3 5 16 4 11 5 20 4 7 ? Answer : 8 In each rectangle, multiply first row numbers and subtract sum of numbers in second row from it. Result of each rectangle should be 40. (13*5)-(5+20) = 40 (12*4)-(3*5) = 40 (16*4)-(20+4) = 40 (11*5)-(7+8)= 40 So, answer is 8. """ Here we test two different representation from the data matrix [[13,5,5,20], [12,4,3,5], [16,4,20,4], [11,5,7,_] ] * Representation 1: data = [[[13,5,5],20], [[12,4,3],5], [[16,4,20],4]] vars = [A,B,C] unknown = [11,5,7] ops = (map)[infix = [+,*,-]] constants = [1,2,4,8,16,32] Some solutions: maxCount = 7 code = ((((A * B) - 32) - 8) - C) = [[[13,5,5],20],[[12,4,3],5],[[16,4,20],4]] = [[11,5,7] = 8] code = ((((A * B) - 8) - C) - 32) = [[[13,5,5],20],[[12,4,3],5],[[16,4,20],4]] = [[11,5,7] = 8] * Representation 2: data = [[[13,12,16],11], [[5,4,4],5], [[5,3,20],7]] vars = [A,B,C] unknown = [20,5,4] ops = (map)[infix = [+,*,-,mod]] constants = [1,2,4,8,16,32] With only +,*,- no solution is found, but adding modulo give some other solutions: maxCount = 5 code = (32 mod (C + (A mod 8))) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 0] code = (32 mod (C + (1 + 4))) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 5] maxCount = 6 maxCount = 7 code = (A + (A mod ((8 mod C) - A))) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 20] code = (A + (C mod ((2 + 4) - B))) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 20] code = (B + (4 - ((C * 32) mod A))) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 1] code = (1 + (32 mod (B + (2 + 8)))) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 3] code = (4 + (((C + 16) - B) mod A)) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 19] code = (4 + ((B - (C * 32)) mod A)) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 21] code = (4 + ((16 + (C mod B)) mod A)) = [[[13,12,16],11],[[5,4,4],5],[[5,3,20],7]] = [[20,5,4] = 4] */ data(squares,Data,Vars,Unknown,Ops,Constants,MaxC) :- Matrix = [[13,5,5,20], [12,4,3,5], [16,4,20,4], [11,5,7,_] ], member(Representation,1..2), Ops = new_map([infix=["+","*","-"] ]), % Ops = new_map([infix=["+","*","-","mod"] ]), Constants = [1,2,4,8,16,32], if Representation == 1 then nl, make_data_matrix(Matrix,Data,Unknown,Vars), MaxC = 10 else nl, get_global_map().put("only_first_solution"), make_data_matrix(Matrix.transp,Data,Unknown,Vars), MaxC = 10 end.