# A truly amazing way of making the number 2016

Find a mathematical expression that yields the value $2016$ while obeying the following rules:

• Each of the digits $1,2,3,4,5,6,7,8,9$ is used exactly once
• Decimal points are allowed
• You may use brackets "(" and ")" to structure your expression, and to make it well-defined
• The only allowed mathematical operations are addition (+), subtraction (-), multiplication (*), division (/)
• The only allowed mathematical functions are square-roots and logarithms. Logarithms must be written in the form $\log[b](x)$ to denote the base-$b$ logarithm of number $x$

Note that in particular the following is not allowed:

• Juxtaposition of digits (as juxtaposing 1 and 3 to get "31")
• other mathematical operations and functions (cube-roots, exponentiation, factorials, absolute values, trigonometric functions, etc)
• matrices and determinants
• integration, differentiation, limits
-
2016 is too easily factorable for this to be a challenge. – Joe Z. Mar 6 at 19:48
Some more restrictive rules could be imposed. – Pietro Majer Mar 6 at 20:18
What do you mean by "decimal points are allowed"? Do you mean numbers like $.7$? Also, you should specify whether or not $0$ is allowed. – LegionMammal978 Mar 6 at 20:37
All the Perry-style solutions proposed so far use only the operations $+$, $-$, and $\times$. There are $\frac{3^8 \cdot 16!}{8! \cdot 9!} = 9,382,230$ such expressions using the digits $1$ to $9$ in sequence. I suppose one could just simple-mindedly calculate the values of all of them, and see which ones work out to be $2016$. It's easily computationally feasible, but it might be fun to try to think of a less stupid method! Or else, it might be idiotic to become obsessed with such a silly idea. I'm trying to make myself not think about it now. Damn. :) – Calum Gilhooley Mar 6 at 23:29
I just want to point it out that if you check this wonderful answer you will realize that there are 366 possible combinations of 2016 using solely the "-", "+" and "*" operations. It happens that 2016 is a leap year, which means you can have 1 solution for every day. I find this more satisfying than I should. – Eduardo Almeida dos Santos Mar 8 at 21:42

$$\frac{9\cdot 8\cdot 7\cdot6\cdot 2}{3}+5-4-1$$

And three more à la Perry

$$1\cdot(2-3+4-5+6)\cdot7\cdot8\cdot 9$$
$$(1+2+3+4+5+6+7)\cdot8\cdot9$$
$$1\cdot(2+3+4+5)\cdot 6 \cdot(7+8+9)$$

-
I particularly like the second of those three 'monotonic' solutions! – Calum Gilhooley Mar 6 at 20:22
the third reminds me of something... – Jon Mark Perry May 20 at 13:06

$(((1-2)\times(3+4))+5+6)\times7\times8\times9$

-
nice monotonicity – Pietro Majer Mar 6 at 14:48

For those interested, here is a brute-forced list of all "monotonic" expressions that use addition, subtraction, multiplication, and division. There's 835 of them in total.

EDIT: For extra fun, here is a list of the 1,151 expressions that use 9 through 1 in order!

-
LOL! It's quite worrying how much this pleases me. :) By the way, what programming language did you use? – Calum Gilhooley Mar 7 at 12:49
@CalumGilhooley I just used a small C# program. I would also include logarithms, but it makes the program explode. – LegionMammal978 Mar 7 at 13:03
It would also be funny looking at monotonic expressions allowing the base 10 notation, like (12+345)*6-... I wonder if there is any giving 2016! – Pietro Majer Mar 8 at 9:28
e.g. 9*(8+6*7*5+4)-3+21 – Pietro Majer Mar 8 at 12:22
@PietroMajer Sadly, that would also make the program explode. – LegionMammal978 Mar 9 at 12:49

Since $2016$ is a multiple of $504 = 7 \times 8 \times 9$, any expression using $1, 2, 3, 4, 5, 6$ that evaluates to $4$ can just multiply those three afterwards and evaluate to $2016$.

So all the following are correct:

$$9 \times 8 \times 7 \times (6 - 5) \times 4 \times (3 - 2) \times 1 = 2016 \\ 9 \times 8 \times 7 \times (6 - 5) \times 4 + 3 - 2 - 1 = 2016 \\ 9 \times 8 \times 7 \times 6 \div (5 + 4) \times (3 + 2 + 1) = 2016$$

There are probably at least fifty more variations that I didn't bother to work out.

-
Similarly, anything with 9 x 8 x 7 x 4 plus any combination that equals zero will work as well. – Cptn_Hammer Apr 18 at 4:18

Using all operations and functions

$9 \cdot 8 \cdot 7 \cdot \sqrt{4} \cdot \log_2(1+\frac{6}{5-3})$

-

A variation on Jon Mark Perry's answer:

$(1 + 2 - 3 - 4) \times (5 - 6) \times 7 \times 8 \times 9$

I wonder how many such 'monotonic' solutions there are?

-
LegionMammal got you covered ;) – DrunkWolf Mar 7 at 19:51

I have found all 3,020 expressions satisfying the given restrictions along with four others:

• The digits 1-9 must be in order.
• You are not allowed to take the square root of $1$ or $0$. Without this restriction we could have arbitrarily complex expressions.
• Every subexpression must have a rational value. This excludes expressions that involve terms like $\log[2]\sqrt{2}=\frac{1}{2}$. Without this restriction we could have arbitrarily complex expressions.
• Decimal points are not allowed, since it is not clear how we are allowed to use them.

My algorithm works as follows. I compute a table of values $\mathtt{terms}$, where each element $\mathtt{terms}[i,j]$ represents the possible values of a subexpression starting at the $i$-th number and spanning $j$ numbers. As an example, take the expression:

$$((1 - (2 \times (3 - 4) + 5)) + 6) \times 7 \times 8 \times 9$$

The term $3-4$ is associated with $\mathtt{terms}[3,2]$, and the term $7 \times 8 \times 9$ is associated with $\mathtt{terms}[7,3]$. The entire expression is associated with $\mathtt{terms}[1,9]$, and the individual numbers are associated with $\mathtt{terms}[*,1]$

Each $\mathtt{terms}[i,j]$ is a list of key-value pairs: the keys are the values of that subexpression, and the values are the ways of reaching that value. For example:

\begin{align} \mathtt{terms}[1, 2] = \{\quad 3 &\to \{1+2\ (1)\},\\ -1 &\to \{1-2\ (1)\}, \\ 2 &\to \{1 \times 2\ (1)\}, \\ \tfrac{1}{2} &\to \{1 \div 2\ (1)\} \quad\} \end{align}

A quick note: for binary operators (all of them except the square root) I store the operator, the left and right-hand operands, and the size of the left operand. The last one is necessary in the second part of the algorithm, and above and below it is shown in parenthesis. Another example:

\begin{align} \mathtt{terms}[1, 3] = \{\quad 6 &\to \{1+5\ (1),\ 1\times 6\ (1),\ 3+3\ (2),\ 2\times 3\ (2)\},\\ 0 &\to \{1+(-1)\ (1),\ 3-3\ (2),\ \sqrt{0}\},\\ 1 &\to \{3\div 3\ (2),\ \log[3](3)\ (2),\ \sqrt{1}\}, \\ \vdots \end{align}

A few notes:

• For all operators, only the values of the operands are stored instead of the entire subexpressions. This is what makes this method efficient.
• Square roots do not need a length stored with them, since they apply to a single subexpression.
• The restriction on square roots of $0$ and $1$ is not applied at this stage (they add at most a constant overhead).

The algorithm that computes $\mathtt{terms}$ is as follows:

• Loop over $\ell=1\ldots 9$:
• Loop over $i=1\ldots 10 - \ell$:
• If $\ell = 1$, initialize $\mathtt{vals}=\{i \to \{i\}\}$.
• Otherwise, initialize $\mathtt{vals}=\{\}$ and loop through $k=1\ldots \ell-1$:
• Get the possible values of the left-hand and right-hand operands. These are: \begin{align}\mathtt{LHS}&=\mathtt{terms}[i,k] \\ \mathtt{RHS}&=\mathtt{terms}[i+k,\ell-k]\end{align}
• Construct all possible terms: $$a\circ b\ (k);\ a\in\mathtt{LHS},\ b\in\mathtt{RHS},\ \circ\in\{+,-,\times,\div,\log\}$$
• Add to $\mathtt{vals}$ all terms that result in a rational value.
• Repeatedly take a the square root of each term in $\mathtt{vals}$. If the result is a rational value, add it to $\mathtt{vals}$.
• Set $\mathtt{term}[i,\ell]=vals$.

Next we traverse $\mathtt{terms}$ in reverse order. Starting with $\mathtt{terms}[1,9]$ we get all the ways to make $2016$, then recursively get all the ways to make each subexpression, ignoring $\sqrt{1}$ and $\sqrt{0}$. This results in a list of 32,282 expressions. I then normalize the expressions using the following transformations:

\begin{align} \cdots+(x_1+\cdots+x_n)+\cdots &\to \cdots+x_1+\cdots+x_n+\cdots \\ \cdots\times(x_1\times\cdots\times x_n)\times\cdots &\to \cdots\times x_1\times\cdots\times x_n\times\cdots \\ \cdots+(x-y)+\cdots &\to ((\cdots+x)-y)+\cdots \\ \cdots\times(x\div y)\times\cdots &\to ((\cdots\times x)\div y)\times\cdots \end{align}

This prevent us from counting expressions like $(1+2)+3$ and $1+(2+3)$ as different. After this step, there are only 3,020 distinct expressions. The full list is available here. Note that this list uses Mathematica notation: $\times\to\mathtt{*},$ $\div\to\mathtt{/},$ $\sqrt{x}\to\mathtt{Sqrt[}x\mathtt{]},$ and $\log[b](x)\to\mathtt{Log[}b\mathtt{,}x\mathtt{]}$.

Of particular note are the expressions:

(1+Log[2,Log[3,4+5]])*6*7*8*Sqrt[9]
(1+Log[2,Log[3,4+5]+6])*7*8*9


(i.e:)

$$(1+\log[2](\log[3](4+5)))\times 6\times 7\times 8\times\sqrt{9} \\ (1+\log[2](\log[3](4+5)+6))\times 7\times 8\times 9$$

Each of which use $\log$ twice, with two different bases!

-
@Legion Yes. I can provide the code if you like. – 2012rcampion Mar 9 at 15:46
@LegionMammal978 It will be much slower if you write code the same way that you write it in, say, C++. However, it can be as fast if you take advantage of Mathematica's high-level functions. – 2012rcampion Mar 10 at 7:20

$(9 \cdot 8 \cdot 7)(6 - 2)\frac{5 - 3}{\sqrt4}\times1$

-

I had code lying around that solves this exact problem, and the first thing it spat out was:

$(1+2+3+4+5+6+7)*8*9$

-
This is very nice, but aside from the personal background, it is a subset of Pietro Majer’s answer from a day earlier; it seems a bit odd to accept this in preference to that. – Peter LeFanu Lumsdaine Mar 9 at 16:16
@PeterLeFanuLumsdaine I agree - I didn't notice the equivalence to one of the answers in Pietro's answer when I wrote it. I probably should have done. After posting, I did find it in the brute forced list linked to in LegionMammal978's answer, but it's buried way down the list in the middle. I think it's the nicest and simplest answer of any of them. – rjmunro Mar 10 at 10:45

$1\cdot2\cdot3\cdot6\cdot7\cdot8+0\cdot4\cdot5\cdot9=2016$; based off of the fact that $2016=2^5\cdot3^2\cdot7$.

If that feels too cheaty, then try this:

$1\cdot2\cdot3\cdot6\cdot7\cdot8+4+5-9=2016$

-
you can't use 0... – Jon Mark Perry Mar 6 at 15:01
@JonMarkPerry Where does it say that? – LegionMammal978 Mar 6 at 15:01
implicitly in the Q – Jon Mark Perry Mar 6 at 15:02
@JonMarkPerry See my second version if you want to be satisfied. – LegionMammal978 Mar 6 at 15:03
Note that log(1)=0, so you could easily convert your first expression into something that works, e.g. log[4](1)*5*9 – Floris Mar 7 at 4:52

My favorite so far:

$9*8*7*6*(5-4)/3*2*1$

Had factorials been allowed, I'd have preferred

$9*8*7*6!/5!*4/3/2*1$

-

In the spirit of the question, I felt it was time for a solution that includes all allowed operations, including a decimal point, a logarithm and a square root (using the hint from Joe Z about the general structure of a solution):

9.*8*7*(-6+5+log[2](4)*3/sqrt(1))

or, with a small tweak from the comment by @f'', this:

9*7*4*(2-3/log[5](sqrt((1+.6)/8)))

which not only has +,-,*,/, decimal, sqrt and log, but makes every operation "do something"

-

$((8+6+3+(2*7))*(9+4)*5)+1 = 2016$

-

Here's mine:

2016 = (9 * 8 * 7 * 4) + 5 * (1 + 2 + 3 - 6)

The first expression is just the factorization of 2016, the rest is just to spend the other digits in a zero-sum.

-

1 * 2 * 3 * 4 * 6 * 7 * 8 / (9-5)

-