Tuesday, August 13, 2024

# Given 4 numbers and a resulting number, create the equation to solve

You’re given four numbers and a result. You are supposed to create an equation with those four numbers that yield that given/desired result. Easy enough. Yeah, right! That’s the kind of puzzles some newspapers are running to challenge the readers as in Epoch Times et al. This is commonly referred to as the 4Numbers game. How do you even start solving this? And can it be solved by code?

Before we begin, let’s make sure you understand the premise, the challenge. There are 4 digits (whole numbers) provided to you, and an output digit (whole number). You are supposed to come up with an equation where the left-hand-side uses ALL of the 4 digits and the right-hand-side produces exactly the output. Your challenge is then is to come up with the equation. For example, you are given a, b, c, d and the result z; your job is to come up with a formula a+b+c+d = z or a*b + d-c = z and so on until you get the exact result z.

Surely, we can do trials and errors manually, but can we write a code to do this for us and spare us all the manual work? Besides, wouldn’t it be more cerebral to write a code that can do it?

It is possible, however, one caveat is that it won’t work with complex calculus. It turns out however, most of these challenges presented as puzzle don’t require that complex calculus. We can use usual arithmetic and basic algebra to solve most of them. Let’s look at some examples.

You are given these four numbers: 3,8,3,5. Your task is to find an equation using those numbers that yields 26.

You are given these four numbers: 4,5,3,3. Your task is to find an equation using those numbers that yields 24.

You are given these four numbers: 1,1,4,6. Your task is to find an equation using those numbers that yields 24.

You are given these four numbers: 3,8,3,6 Your task is to find an equation using those numbers that yields 12.

You are given these four numbers: 3,3,5,7 Your task is to find an equation using those numbers that yields 28.

Okay, you get idea. So, please go ahead and try to solve the above ones yourself for the joy of the puzzle. Once you’re done and verified the result, look at my script below that solves these instantly and shows you the exact solution formulas, so you can compare. Click on below and enter your four digits as described above and separate each number by a comma, and hit ENTER/Return on your keyboard. Then in the next prompt, enter the desired result (as shown above). Of course, you can enter any number and desired output, but realize that there will no possible solutions for some inputs no matter what math you conjure up! If there is no possible solution, the program will output: `"No valid equation found."`

To run it again, press Run again for each new challenge. Have fun!

## The Solutions

Here are the actual inputs and solved outputs of the challenges posed above.

Notice how every number in the input are used on the left-hand-side of the equation to produce the exact result desired.

Can you come up with a formula using these numbers: 3,8,3,5 that result in 48? What does it look like?

I hope this fun and intriguing. If you would like to get the full source code, please contact me at trseattle at outlook dot com. Readers with confirmed donations will get the source code for free of charge upon request. Thanks for reading!