/* From Muhammad Zain Sarwar: "Finding the Missing Numbers in the Sequence?" https://medium.com/puzzle-sphere/finding-the-missing-numbers-in-the-sequence-a9e607a2d844 """ 5,10,12,24,26,52,54,?,? """ The answer is 108,110 according to these operations: 5*2=10 10+2=12 12*2=24 24+2=26 26*2=52 52+2=54 54*2=108 108+2=110 This was much easier to solve manually than writing a program for. :-) The problem here is the representation of the problem since it's not the same operation for each step (which is the standard approach), but two. * Let's start with the simple representation of a "point sequence", i.e. the dependent variable only rely on the indices. data = [[[1],5],[[2],10],[[3],12],[[4],24],[[5],26],[[6],52],[[7],54]] This does not work. * Let's now try with a recursive sequence: data = [[[5],10],[[10],12],[[12],24],[[24],26],[[26],52],[[52],54]] This is better: It actually give the next value 54 -> 108. [program = if_then_else((A + 10) mod 7,2 * A,A + 2),res = 108,count = 757] The if_then_else/3 function is interpreted as if Cond > 0 then Expr1 else Expr Test: A = 54 if_then_else ((A + 10) mod 7) > 0 then 2 * A else A + 2 end if ((54 + 10) mod 7) > 0 then 2*54 else 54 + 2 end -> 108. Correct Now we test the second, with A = 108 if ((108 + 10) mod 7) > 0 then 2*108 else 109 + 2 end -> 216. Incorrect! It fails since it cannot distinguish between the odd'th or even'th expressions. * Another representation introduces indices: Index, This -> Next and with mod/2 and cond/3 Data = [ [[1,5],10], [[2,10],12], [[3,12],24], [[4,24],26], [[5,26],52], [[6,52],54] Expected program: * cond(I mod 2==1,A*2,A+2) * if I mod 2 == 1 then A*2 else A+2 end Here are some for if_then_else/3: [program = if_then_else(I mod 2,A * if_then_else(A,if_then_else(8,2 - I,I),2),5 mod 3 + A mod (4 * A)),res = 108,count = 630] [program = if_then_else(I mod 2,2 * A,5 mod 3 + A mod (4 * A)),res = 108,count = 560] [program = if_then_else(I mod 2,2 * A,A + 2),res = 108,count = 479] [program = if_then_else(I mod 2,A + A,5 mod 3 + A mod (4 * A)),res = 108,count = 462] [program = if_then_else(I mod 2,A * if_then_else(A,if_then_else(8,2 - I,I),2),A + 2),res = 108,count = 427] [program = if_then_else(I mod 2,if_then_else(5 + A,if_then_else(A + 5 * A,7,A),A) + A,5 mod 3 + A mod (4 * A)),res = 108,count = 371] [program = if_then_else(I mod 2,A + A,A + 2),res = 108,count = 333] [program = if_then_else(I mod 2,if_then_else(5 + A,if_then_else(A + 5 * A,7,A),A) + A,A + 2),res = 108,count = 285] [program = if_then_else(I mod 2,A * if_then_else(A,if_then_else(8,2 - I,I),2),2 + A),res = 108,count = 266] [program = if_then_else(I mod 2,2 * A,2 + A),res = 108,count = 221] [program = if_then_else(I mod 2,A * 2,A + 2),res = 108,count = 210] [program = if_then_else(I mod 2,A * 2,2 + A),res = 108,count = 192] [program = if_then_else(I mod 2,A * 2,5 mod 3 + A mod (4 * A)),res = 108,count = 182] [program = if_then_else(I mod 2,A + A,2 + A),res = 108,count = 84] [program = if_then_else(I mod 2,if_then_else(5 + A,if_then_else(A + 5 * A,7,A),A) + A,2 + A),res = 108,count = 2] [program = if_then_else(if_then_else(I mod I,9 * 7,if_then_else(9,5,I) mod 2),A * if_then_else(A,if_then_else(8,2 - I,I),2),A + 2),res = 108,count = 1] [program = if_then_else(if_then_else(I mod I,9 * 7,if_then_else(9,5,I) mod 2),A * if_then_else(A,if_then_else(8,2 - I,I),2),2 + A),res = 108,count = 1] Let's check with this program: if_then_else(I mod 2,A + A,A + 2) a) I = 7, A = 54 if_then_else( (7 mod 2) > 0 then 54+54 else 54+2) -> 108. Correct b) I = 8, A = 108 if_then_else( (8 mod 2) > 0 then 108+108 else 108+2) -> 110. Correct */ data(missing_numer,Data,Vars,Unknown,Ops,Constants,MaxSize,Params) :- % make_point_seq([5,10,12,24,26,52,54],Data,Unknown,Vars), % nope % make_seq([5,10,12,24,26,52,54],1,Data,Unknown,Vars), % first ok, second not ok Data = [ [[1,5],10], [[2,10],12], [[3,12],24], [[4,24],26], [[5,26],52], [[6,52],54] % , [[7,54],108] % checking ], Vars = ['I','A'], Unknown = [7,54], % should be 108 % Unknown = [8,108], % checking: should be 110 Ops = [+,*,-,mod,if_then_else,==], % Ops = [+,*,-,mod,cond,==], Constants = 1..10, MaxSize = 11, Params = new_map([init_size=1000, num_gens=1000 ]).