This answer is based on the accepted answer to this question on math.stackexchange:
https://math.stackexchange.com/questions/354625/how-to-compose-given-add-sub-mult-div-functions-to-map-an-integer-m-to-n
Given the six inputs (a, b, c, d, M, N)
- a: the number we can add
- b: the number we can subtract
- c: the number we can multiply
- d: the number we can divide
- M: the starting number
- N: the ending number
I assume they are all integers, and a > 0, b > 0, c could be positive or negative, d not equal to 0.
Then there is a way to transform M to N by applying add, sub, mult, div,
if and only if
there exist integers m and n such that

So, the first thing to do is to find the greatest common divisor of a and b (I recommend Euclid's algorithm as described here. Call the gcd "g".
The mod is applied to both sides to verify equality.
So, start making a list of possible values the left hand side can take for different m:
(M*( Pow(c,m) )) % g
Then, start making a list of possible values the right hand side can take for different n:
(N*( Pow(d,n) )) % g
You will want to start m and n at zero and work your way up.
Eventually, both sides will start repeating, since you are using mod.
If at any time you find a match, i.e. left hand side equals some right hand side, return 1.
Otherwise, once you have exhausted all left hand values and right hand values, with no matches, return 0.
Note, that the mod operator (%) in C++ does behave different for negative numbers than the above math describes. You will need to adjust the results of mod to always be in the range 0 <= result < g
Lastly, this only works fully for cases where division acts according to normal math, i.e. 3 / 2 = 1.5 and so on.
In your case, you would need to modify the formula slightly to accept integer division. In particular the right hand side of the equation deals with division. Kind of like how the following equation works, we can take a divide, move it to the other side and it becomes a multiply.
x / 3 = 1
x = 1*3
You will need to allow the rhs to take multiple values each time you perform a power on d. For example, in the above example, we need to allow 1*3 to equal 3,4, or 5.
So, if d = 3, then
- Pow(d,0) = 1.
- Pow(d,1) = 3 or 4 or 5.
- Pow(d,2) = 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17
You will see that 9 = 3*3, 12 = 4*3, and 15 = 5*3. So the pattern should be easy to replicate, even for different values of d.
I haven't tried this last part out, so I'm not entirely sure this exactly covers all cases of the integer division, but I think it does, although it is kind of tedious.