07/02/22 Computing Challenge

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:

07-02-22-arithmetic-puzzleWrite 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

Solution

Check the code below that uses the random library to try a range of arithmetic expressions until a solution is found…

Your task…

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!

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.4 / 5. Vote count: 8

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!