I would like to use Mathematica to create a simple program in which we can find all ways to construct 21 by using each of 1, 5, 6, and 7 exactly once. The numbers may be combined in any way using any of the primary binary operators, i.e. addition, subtraction, multiplication, and division, as well as parenthesis. I feel that the method of brute-forcing all possible combinations of 1, 5, 6 and 7 would likely be effective, but I am having trouble structuring the code. My approach would likely be the following:
- Define a vector
S = (1, 5, 6, 7)
- Remove any two numbers in
S
and perform an arbitrary binary operation on the two numbers, obtaining a new numbera
.- Replace
a
intoS
, generatingS'
.- Repeat from step 1 until this particular brute-force branch generates a final value.
Hence, the cycle would repeat until S reduces to one element. If the branch results in 21, we would be done. The problem is I do not know how to code steps 2 and 3 in Mathematica, and would like some help on how to structure my code, or if my approach isn't efficient, on changing up my approach.