-- more about the SQL: -- https://www.orczhou.com/index.php/2024/03/a-sql-for-24-point-game/ select id,t.c1,t.c2,t.c3,t.c4, ( select result_expr FROM ( select @if_found := false, @r_expr := 'failed', t_each_row.id,c1,c2,c3,c4, op_1st,op_2nd,op_3rd, @c_1 := case full_order.seq_num_1 when 1 then c1 when 2 then c2 when 3 then c3 when 4 then c4 END as c_1, @c_2 := case full_order.seq_num_2 when 1 then c1 when 2 then c2 when 3 then c3 when 4 then c4 END as c_2, @c_3 := case full_order.seq_num_3 when 1 then c1 when 2 then c2 when 3 then c3 when 4 then c4 END as c_3, @c_4 := case full_order.seq_num_4 when 1 then c1 when 2 then c2 when 3 then c3 when 4 then c4 END as c_4, -- , @c_1,@c_2,@c_3,@c_4, /* total 5 trees */ /*left most tree*/ /* ((@c_1 op_1 @c_2) op_2 @c_3) op_3 @c_4 */ @lt_1 := case op_1st when '*' then @c_1 * @c_2 when '+' then @c_1 + @c_2 when '-' then @c_1 - @c_2 when '/' then @c_1 / @c_2 END as lt_1, @lt_2 := case op_2nd when '*' then @lt_1 * @c_3 when '+' then @lt_1 + @c_3 when '-' then @lt_1 - @c_3 when '/' then @lt_1 / @c_3 END as lt_2, @lt_3 := case op_3rd when '*' then @lt_2 * @c_4 when '+' then @lt_2 + @c_4 when '-' then @lt_2 - @c_4 when '/' then @lt_2 / @c_4 END as lt_3, @lt_expr := concat("((", @c_1 ,op_1st,@c_2 ,")",op_2nd,@c_3,")",op_3rd,@c_4), if(@lt_3 between 24-0.0001 and 24+0.0001, @if_found := true, 0), if(@lt_3 between 24-0.0001 and 24+0.0001, @r_expr := @lt_expr, 0), /* bushy tree 00 */ /* (c1 op_1st c2) op_2nd (c3 op_3rd c4) */ if( @if_found = false, @bt_1 := case op_1st when '*' then @c_1 * @c_2 when '+' then @c_1 + @c_2 when '-' then @c_1 - @c_2 when '/' then @c_1 / @c_2 END, 0 ) as bt_1, if( @if_found = false, @bt_2 := case op_3rd when '*' then @c_3 * @c_4 when '+' then @c_3 + @c_4 when '-' then @c_3 - @c_4 when '/' then @c_3 / @c_4 END, 0 ) as bt_2, if( @if_found = false, @bt_3 := case op_2nd /* '+' & '*' there is always a equel tree */ when '*' then @bt_1 * @bt_2 when '+' then @bt_1 + @bt_2 when '-' then @bt_1 - @bt_2 when '/' then @bt_1 / @bt_2 END, 0 ) as bt_3, @bt_expr := concat("(",@c_1,op_1st,@c_2,")",op_2nd,"(",@c_3,op_3rd,@c_4,")"), if(@bt_3 between 24-0.0001 and 24+0.0001, @if_found := true , 0), if(@bt_3 between 24-0.0001 and 24+0.0001, @r_expr := @bt_expr, 0), /*right most tree*/ /* c1 op_1 (c2 op_2 (c3 op_3 c4)) */ if( @if_found = false, @rt_1 := case op_3rd when '*' then @c_3 * @c_4 when '+' then @c_3 + @c_4 when '-' then @c_3 - @c_4 when '/' then @c_3 / @c_4 END, 0 ) as rt_1, if( @if_found = false, @rt_2 := case op_2nd when '*' then @c_2 * @rt_1 when '+' then @c_2 + @rt_1 when '-' then @c_2 - @rt_1 when '/' then @c_2 / @rt_1 END, 0 ) as rt_2, if( @if_found = false, @rt_3 := case op_1st when '*' then @c_1 * @rt_2 when '+' then @c_1 + @rt_2 when '-' then @c_1 - @rt_2 when '/' then @c_1 / @rt_2 END, 0 ) as rt_3, @rt_expr := concat(@c_1, op_1st, "(", @c_2 ,op_2nd, "(",@c_3, op_3rd, @c_4,")",")"), if(@rt_3 between 24-0.0001 and 24+0.0001, @if_found := true, 0), if(@rt_3 between 24-0.0001 and 24+0.0001, @r_expr := @rt_expr, 0), /* bushy tree 01 */ /* (c2 op2 (c3 op3 c4)) op1 c1 */ if( @if_found = false, @bt01_1 := case op_3rd when '*' then @c_3 * @c_4 when '+' then @c_3 + @c_4 when '-' then @c_3 - @c_4 when '/' then @c_3 / @c_4 END, 0 ) as bt01_1, if( @if_found = false, @bt01_2 := case op_2nd when '*' then @c_2 * @bt01_1 when '+' then @c_2 + @bt01_1 when '-' then @c_2 - @bt01_1 when '/' then @c_2 / @bt01_1 END, 0 ) as bt01_2, if( @if_found = false, @bt01_3 := case op_1st /* '+' & '*' there is always a equel tree */ when '*' then @bt01_2 * @c_1 when '+' then @bt01_2 + @c_1 when '-' then @bt01_2 - @c_1 when '/' then @bt01_2 / @c_1 END, 0 ) as bt01_3, @bt01_expr := concat("(",@c_2, op_2nd , "(" ,@c_3, op_3rd, @c_4, "))", op_1st, @c_1 ), if(@bt01_3 between 24-0.0001 and 24+0.0001 , @if_found := true , 0), if(@bt01_3 between 24-0.0001 and 24+0.0001 , @r_expr := @bt01_expr, 0), /* bushy tree 02 */ /* c1 op1 ((c3 op3 c4) op2 c2) */ /* @c_1 op_1st (( @c_3 op_3rd @c_4) op_2nd @c_2 ) */ if( @if_found = false, @bt02_1 := case op_3rd when '*' then @c_3 * @c_4 when '+' then @c_3 + @c_4 when '-' then @c_3 - @c_4 when '/' then @c_3 / @c_4 END, 0 ) as bt02_1, if( @if_found = false, @bt02_2 := case op_2nd when '*' then @bt02_1 * @c_2 when '+' then @bt02_1 + @c_2 when '-' then @bt02_1 - @c_2 when '/' then @bt02_1 / @c_2 END, 0 ) as bt02_2, if( @if_found = false, @bt02_3 := case op_1st /* '+' & '*' there is always a equel tree */ when '*' then @c_1 * @bt02_2 when '+' then @c_1 + @bt02_2 when '-' then @c_1 - @bt02_2 when '/' then @c_1 / @bt02_2 END, 0 ) as bt02_3, @bt02_expr := concat( @c_1, op_1st, "((", @c_3, op_3rd, @c_4,")", op_2nd, @c_2, ")"), if(@bt02_3 between 24-0.0001 and 24+0.0001 , @if_found := true , 0), if(@bt02_3 between 24-0.0001 and 24+0.0001 , @r_expr := @bt02_expr, 0), if(@if_found , @r_expr , "false") as result_expr, @if_found as if_found from (select t.id,t.c1,t.c2,t.c3,t.c4) as -- (select 9 as id,9 as c1,3 as c2,1 as c3,10 as c4, @if_found := false) as t_each_row , ( WITH RECURSIVE seq (n) as ( SELECT 1 UNION ALL SELECT n + 1 FROM seq WHERE n <= 3 ) select seq_1.n as seq_num_1, seq_2.n as seq_num_2, seq_3.n as seq_num_3, seq_4.n as seq_num_4 from seq as seq_1, seq as seq_2, seq as seq_3, seq as seq_4 WHERE pow(2,seq_1.n-1)+pow(2,seq_2.n-1)+pow(2,seq_3.n-1)+pow(2,seq_4.n-1) = 15 ) full_order , ( WITH op_list (op) as ( SELECT '*' UNION SELECT '+' UNION SELECT '-' UNION SELECT '/' ) SELECT op_1.op as op_1st,op_2.op as op_2nd,op_3.op as op_3rd FROM op_list as op_1,op_list as op_2,op_list as op_3 ) full_op ) mid_result WHERE result_expr != "false" LIMIT 1 ) mid_result_01 from cards as t