Four fours
Four fours is a mathematical puzzle. The goal of four fours is to find the simplest mathematical expression for every whole number from 0 to some maximum, using only common mathematical symbols and the digit four. Most versions of four fours require that each expression have exactly four fours, but some variations require that each expression have the minimum number of fours. This game requires skill.
The first printed occurrence of the specific problem of four fours is in in 1881.
W. W. Rouse Ball described it in the 6th edition of his Mathematical Recreations and Essays. In this book it is described as a "traditional recreation".
Rules
There are many variations of four fours; their primary difference is which mathematical symbols are allowed. Essentially all variations at least allow addition, subtraction, multiplication, division, and parentheses, as well as concatenation. Most also allow the factorial, exponentiation, the decimal point and the square root operation. Other operations allowed by some variations include the reciprocal function, subfactorial, overline, an arbitrary root, the square function, the cube function, the cube root, the gamma function, where Γ =, and percent. Thusetc.
A common use of the overline in this problem is for this value:
Typically the "log" operators or the successor function are not allowed, since there is a way to trivially create any number using them. This works by noticing 3 things:
1) you can take square roots repeatedly without using any additional 4s
2) a square root can also be written as the exponent
3) exponents have logarithms as their inverse.
Writing repeated square root in this form we can isolate n, which is the number of square roots!:
we can isolate both exponents by using log base 4
we can think of this log base 4 as the question--"4 to what power gives me 4 to the half power to the n power?"
so we are now left with:
and now we can do the same thing to isolate the exponent, n:
so, putting it all together:
Now, we can rewrite the base with only 4s and the exponent back to a square root:
We've used four fours and now the number of square roots we add equals whatever number we want out!
Paul Bourke credits Ben Rudiak-Gould with a different description of how four fours can be solved using natural logarithms to represent any positive integer n as:
Additional variants replace the set of digits with some other set of digits, say of the birthyear of someone. For example, a variant using "1975" would require each expression to use one 1, one 9, one 7, and one 5.
Solutions
Here is a set of four fours solutions for the numbers 0 through 32, using typical rules. Some alternate solutions are listed here, although there are actually many more correct solutions. The entries in blue are those that use four integers 4 and the basic arithmetic operations. Numbers without blue entries have no solution under these constraints. Additionally, solutions that repeat operators are marked in italics.0 44 − 44
1 44 ÷ 44
2 ÷ 4!
3
4 −44 + 4! + 4!
5 ÷ 4
6 ' 4.4 + 4 ×.4
7 44 ÷ 4 − 4
8 4.4 −.4 + 4
9 ' 44 ÷ 4 − √4
10 4 ÷√4 + 4 ×√4 ÷ 4
11 ÷ 4 √4 × ÷ 4
12 ÷ 4
13 ÷ 4 ÷.4 + 4
14 4 × 4 − 4 ÷√4 4 × − √4
15 44 ÷ 4 + 4
16 ×.4
17 ÷ 4
18 4 × 4 + 4 −√4 − 4
19 4!− ÷.4
20 ÷ √4
21 4!− 4 + 4 ÷ 4 ÷ √4
22 4!÷ 4 + 4 × 4 44 ÷
23 4!+ 4 ÷ 4 −√4 ÷ √4
24 ÷ √4
25 4!− 4 ÷ 4 +√4 ÷.4
26 4!+ √4 + 4 - 4
27 4!+ √4 +
28 4!+ 4 + 4 - 4
29 4!+ 4 +
30 4!+ 4 + 4 - √4
31 4!+ ÷4.
32
There are also many other ways to find the answer for all of these.
Note that numbers with values less than one are not usually written with a leading zero. For example, "0.4" is usually written as ".4". This is because "0" is a digit, and in this puzzle only the digit "4" can be used.
A given number will generally have a few possible solutions; any solution that meets the rules is acceptable. Some variations prefer the "fewest" number of operations, or prefer some operations to others. Others simply prefer "interesting" solutions, i.e., a surprising way to reach the goal.
Certain numbers, such as 113, are particularly difficult to solve under typical rules. For 113, Wheeler suggests. A non-standard solution is, where 4' is the multiplicative inverse of 4. Another possible solution is, where and represent the 10th and 127th multifactorials respectively, and should technically be denoted with that many exclamation marks to adhere to the rules of the problem.
The use of percent admits solutions for a much greater proportion of numbers; for example, 113 = ÷ %.
The number 157 can be solved using the gamma function, one of the possible solutions is - 4!
Algorithmics of the problem
This problem and its generalizations may be solved by a simple algorithm. The basic ingredients are hash tables that map rationals to strings. In these tables, the keys are the numbers being represented by some admissible combination of operators and the chosen digit d, e.g. four, and the values are strings that contain the actual formula. There is one table for each number n of occurrences of d. For example, when d=4, the hash table for two occurrences of d would contain the key-value pair 8 and 4+4, and the one for three occurrences, the key-value pair 2 and /4.The task is then reduced to recursively computing these hash tables for increasing n, starting from n=1 and continuing up to e.g. n=4. The tables for n=1 and n=2 are special, because they contain primitive entries that are not the combination of other, smaller formulas, and hence they must be initialized properly, like so
T := "4";
T := ".4";
T := ".4...";
and
T := "44";.
. Now there are two ways in which new entries may arise, either as a combination of existing ones through a binary operator, or by applying the factorial or square root operators. The first case is treated by iterating over all pairs of subexpressions that use a total of n instances of d. For example, when n=4, we would check pairs into the hash table, including parenthesis, for n=4. Here the sets A and B that contain a and b are calculated recursively, with n=1 and n=2 being the base case. Memoization is used to ensure that every hash table is only computed once.
The second case is treated with the help of an auxiliary function, which is invoked every time a value v is recorded. This function computes nested factorials and roots of v up to some maximum depth, restricted to rationals.
The last phase of the algorithm consists in iterating over the keys of the table for the desired value of n and extracting and sorting those keys that are integers. This algorithm was used to calculate the five fives and six sixes examples shown below. The more compact formula was chosen every time a key occurred more than once.
Excerpt from the solution to the five fives problem
139 =140 =
141 = !+)
142 = !+)
143 = /5)
144 = )!/5)
145 =
146 = !++))
147 = !+)
148 =
149 = )
Excerpt from the solution to the six sixes problem
In the table below, the notation.6... represents the value 6/9 or 2/3.241 = *))/.6)
242 = -)
243 = )
244 = )
245 = !+)/6)-6)
246 = ))
247 = )/6))
248 = ))
249 = )))
250 =
251 = -)