A long time ago, my brilliant daughter was in the Gifted-and-Talented (G.T.) program at her school. She had an assignment to find an equation for each number from 0 to 50 with the rules being that the digit 4 must appear 4 times in the equation, and that there be no other digits.

We had some trouble with this puzzle at first. But once we got started, it was clear that this was not a job for humans. So I wrote a program to do it. I never knew that this was a common puzzle; we got much better at using Google to find out about her school work when she got into high school.

I actually computed equations for all values up to 1000, but only values up to 50 are listed here. The entire output file can be found here. There are some answers that my code could not compute, starting at 73.

This was not one of my best-written programs. I tried to get it done as quickly as possible. The only part of it that I tried to do well was the conversion to fixed-point math, and I only did that yesterday so that I could add the ability to use .4 in the equations.

Ah, .4 you say? Isn’t that supposed to be 0.4, which violates the 4-digit rule? Yes, it violates that rule if written as 0.4. Here is a link to a mathforum.org page which says that there should be a leading zero, and therefore, no 0.4 in any equation. I didn’t look further than this. But in the end, it is ok to include .4 because there is certainly no rule in math against leaving off the leading zero.

Like I mentioned before, the code is not pretty. I fit it into another program that I already had written. This is written in C++ using MFC on Windows.

The way the code works is this: Each different number that can be represented by only fours is in the Data array at line 11. There is no reason to have more than one way to compute the number 1 in any of these equations so that one possibility is in the list. The code simply steps through each possible combination of these numbers and math operators and then keeps those that meet the rules.

A little more detail: An array of 7 numbers is used for the iteration. The even numbered positions in the array can have values of 0 though 9, and the odd positions can have values 0 through 3. The 0 through 9 values represent the positions within the Data array and the 0 though 3 values represent +, –, *, / operations. The array is “incremented” like a decimal number, where the ones column is incremented until it gets past 9 and then it is set back to 0 and the tens column is incremented by 1. The equation array (line 92) works the same way but 3 is the highest value of the odd numbered positions in the array.

{ 5, 1, 5, 3, 4, 2, 0 } represents the equation ( ( ( 4 * 4 ) / (44/4) ) – 444 ) which equals –442.545454.

Each equation is analyzed to see if the result is an integer (whole number) and if there are exactly four 4’s being used. Back in the Data array, the number of 4’s is there to make this calculation easier. The best equation for a number is saved and they are all saved to a file later.

Don’t ask me what the rating means. I think that I wanted to keep the “best” equation but I never clearly defined what it was to be best. I could have just kept the first one and then changed the order of the Data array to have the best components be earliest in the array.

The code has some bugs. For example, I don’t use the correct count for the main loop, which I just noticed a moment ago.

I used fixed-point math because .4 is actually 0.40000000000000002 in IEEE double precision floating point representation. I didn’t want this to screw up my results. The fixed-point might cause some values to end up looking like integers that are not.

One more note; I only use the sqrt() function. I don’t use log(), gamma(), or other functions. There are probably lots of available functions but they were not on the allowed list for the class project.

Improvements

I would love to improve this to the point where I could create the most complete list of results ever. I would need to add more functions than just sqrt(). I would also want to see if using parentheses in the equation building could give me more interesting results or are redundant due to my using a list of equation parts (the Data array) that contains parentheses when needed.

I’m playing with the code right now and I see that after I added .4 to the mix, that I could then add (4/.4) to the list of components so that I can have a value of 10 calculated. But I wonder if there is any point in having that, since the rest of the code should find it anyhow during the equation evaluations.

But for now, this is a pretty code bit of code considering that I spent an evening on it, hurrying the whole time.