I am not sure what is the real bug in here but I do know one thing.
you should not work forwards in the list of numbers to see if it equates.
Instead you should work from the last number back to the first one. This is because if the each number tested cannot divide without a remainder or be subtracted to 0 or above then the list of numbers is entirely invalid.
This is a state solver type problem. You are basically bruteforcing each possible state and seeing if one is valid. This is computationally expensive.
take a look at the example:
3267: 81 40 27
81 + 40 * 27
and 81 * 40 + 27
both equal 3267
when evaluated left-to-right
So working backwards from the final state 3267
to the first state in the list 81
:
3267 / 27 = 121 - 40 = 81
and so it is valid!
3267 - 27 = 3240 / 40 = 81
Also valid!
but here are also examples that show being invalid:
192: 17 8 14
192 / 14 = 13.7
Invalid, skip checking the rest to save time!
192 - 14 = 178 / 8 = 22.25
invalid! needs to be the same as the first number in the list and cannot have a remainder!
192 - 14 = 178 - 8 = 170 != 17
invalid! needs to be the same as the first number in the list!
this exhausts all paths that can be created!