How to create an algorithm to find all combinations for a given formula (add 2 or subtract 3)
10:20 02 Jul 2025

What I have is the following 'formula' : < +2 or -3 > (for example).

One can 'add 2 to' or 'subtract 3 from' a starting digit recursively to form a next digit and add that digit to the the output. Then you can use the new digit for the next iteration.

So when the start digit is 1, that will be added tot the empty output, so the output will then be '1', then the only posibility is to add 2 to 1, so the next digit is 3, which will be added to the output, making the output '13' from here, the next posibility is again an add action, so the next digit will be 5, and the output will be '135', after this moment there are 2 possibilities, to add again so the next digit will be 7, another way is to subtract 3 so the next digit will be 2, so the output can be '1357' or it can be '1352', etc.

The restrictions are not so complicated, one can only use the digits 1 to 9, and one can never reach the same digit within the same solution. What I want to find is all possible combinations with a certain starting digit.

To find the first combination I add 2 to the previous digit as long as possible, and then try to subtract 3, going back to adding 2, with the restriction that you can not use the same digit again, it gives:

1 + 3 + 5 + 7 + 9 + 6 + 8 -> (4 times add 2, 1 time subtract 3, and 1 time add 2) 

But the second (starting with an 1) will be:

1 + 3 + 5 + 7 + 4 + 6 + 8 -> (3x +2, 1x -3, and 2x +2)

and the third solution will be

1 + 3 + 5 + 2 + 4 + 6 + 8 -> (2x +2, 1x -3, and 3x +2)

so I find 3 solutions, all with a length of 7, doing this with only my brains:

'1357968', '1357468', '1352468'

As far as I see, there are 3 possibilities when starting with a 1. I am researching a solution to find all posibilities with a start - digit.

The version starting with an 1 is easy to do with only a pen and paper, but other starting digits can be a bit more difficult. My question is if there is some theory about this kind of 'formula's' or a research area, can someone get me some information to help me to develop an algoritm?

algorithm formula