As this post was published on the 7th of February 2022 (07/02/22), we would like to use a Python algorithm to solve the following arithmetic puzzle:
Write an arithmetic expression using the number 2 exactly 7 times, using any of the following arithmetic operators: +, -, *, /, ^.
Your arithmetic expression will need to give a result of 22.
7th February 2022
Note that, for this challenge, we will not be using brackets: we will complete the operations from left to right without considering the rules of operator precedence.
So for instance:
- Without operator precedence: 2 + 2 + 2 * 2 = 4 + 2 * 2 = 6 * 2 = 12
- Whereas, when applying the rules of operator precedence: 2 + 2 + 2 * 2 = 2 + 2 + 4 = 4 + 4 = 8
Check the code below that uses the random library to try a range of arithmetic expressions until a solution is found…
Write a different algorithm that does not rely on using the random library but instead tries every possible expression and outputs all the possible solutions!
In total, there should be 56 = 15,625 possible expressions to check!