# Four dice puzzle: 2,2,4,5

Damiano has thrown four dice and the numbers 2, 2, 4, 5 showed up on top.

What is the smallest positive integer that cannot be generated with these four numbers according to the following rules?

• One may use the four numbers 2, 2, 4, 5, and it is fine if not all of them are used.
• Concatenation of digits is NOT allowed.
• The only allowed operations are additions, subtraction, multiplication, and division ($+,-,*,/$).
• One may use any number of brackets.

Examples: $~~~~1=5-4$; $~~~~2=4-2$; $~~~~3=4-(2/2)$; $~~~~4=(4+2)-2$; $~~~~$etc.

23.

Joel Rondeau’s answer gives ways of getting anything up to this, except for 17; but 17 = ((4+2)*2)+5.

If I am not mistaken, the full range of positive integer values achievable is

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,24,25,26,28,30,32,36,38,40,42,44,48,50,56,60,80]

(found using a small Haskell script).

1 = ((2-4)*2)+5
2 = ((2/2)-4)+5
3 = ((4-5)+2)+2
4 = ((5-4)*2)+2
5 = ((2-4)+2)+5
6 = ((2/4)*2)+5
7 = ((4+2)*2)-5
8 = ((5*2)-4)+2
9 = ((2-2)+4)+5
10 = ((2/2)+4)+5
11 = ((4*2)-2)+5
12 = ((4-2)*5)+2
13 = ((2+2)+4)+5
14 = ((5-2)*4)+2
15 = ((4*2)+2)+5
16 = ((5*2)+4)+2
17 = ((4+2)*2)+5
18 = ((5+2)*2)+4
19 = (5*4)-(2/2)
20 = ((5+4)*2)+2
21 = ((2+2)*4)+5
22 = ((5+4)+2)*2
24 = ((5*4)+2)+2
25 = ((2/2)+4)*5
26 = ((4*2)+5)*2
28 = ((5*2)+4)*2
30 = ((5+2)*4)+2
32 = ((4+2)*5)+2
36 = ((5+2)+2)*4
38 = ((5*4)*2)-2
40 = ((2+2)+4)*5
42 = ((5*4)*2)+2
44 = ((5*4)+2)*2
48 = ((5*2)+2)*4
50 = ((4*2)+2)*5
56 = ((5+2)*4)*2
60 = ((4+2)*2)*5
80 = ((2+2)*4)*5

• A lot of these can be made more readable. For example: ((5-2)X4)+2 could simply be (4-2)X(5+2) Oct 10, 2015 at 20:02
• @warspyking: yes, the script just gives the first way it found for each output, not in any way the simplest. And the first sums it tried were the ones bracketed like ((x @ y) @ z) @ w), so most come out this way. Actually, I hadn’t noticed at first, but almost every number that can be reached was reached by that bracketing — the only exception was 19. Oct 11, 2015 at 20:45
• Write the script so that it tries without bracketing first? Oct 12, 2015 at 1:13
• @warspyking: right — if I was doing it again, I’d change the order it tried things in. But I didn’t save the script, and I can’t quite be bothered to write it again now just to get slightly simpler-looking expressions. Oct 12, 2015 at 8:16
• Tell me how you did it specifically, I might write it up in Lua Oct 12, 2015 at 13:13

I don't know if this is what you we going for or not, but assuming we can only use each operator once (there's no rule specified for this)

17

List:

2*(5-4)/2 = 1
(2-5+4)*2 = 2
(2*5-4)/2 = 3
2+(5-4)*2 = 4
2+5-4/2 = 5
(2+5-4)*2 = 6
(2*5+4)/2 = 7
2*5-4+2 = 8
2*(5+4)/2 = 9
(2+5)*2-4 = 10
(2+5*4)/2 = 11
2+5*4/2 = 12
2*(5/2+4) = 13
(2+5)*4/2 = 14
(2+4)5/2 = 15
2
(5+4)-2 = 16
(2+5/2)*4 = 18
4*5-2/2 = 19
2+5*4-2 = 20
2/2+5*4 = 21
(2/2+5)*4 = 24
(2/2+4)*5 = 25
(2+5)*4-2 = 26
(2+4)*5-2 = 28

Also relevant: Your rule about not using each one is irrelevant, any number you can make with less can be made with all of them, at least, up to the smallest number

And if anybody's interested, here's the Lua script used to achieve such results:

local Numbers = {
{2,5,4,2},
{2,5,2,4},
{2,2,5,4},
{2,2,4,5},
{2,4,5,2},
{2,4,2,5},
{4,2,5,2},
{4,2,2,5},
{4,5,2,2},
{5,2,4,2},
{5,2,2,4},
{5,4,2,2}
}

local Operators = {
{'+','/','','-'},
{'+','/','-','
'},
{'+','-','/',''},
{'+','-','
','/'},
{'+','','/','-'},
{'+','
','-','/'},
{'-','+','','/'},
{'-','+','/','
'},
{'-','/','+',''},
{'-','/','
','+'},
{'-','','+','/'},
{'-','
','/','+'},
{'','+','/','-'},
{'
','+','-','/'},
{'','-','+','/'},
{'
','-','/','+'},
{'','/','+','-'},
{'
','/','-','+'},
{'/','+','','-'},
{'/','+','-','
'},
{'/','-','+',''},
{'/','-','
','+'},
{'/','','+','-'},
{'/','
','-','+'}
}

function BracketForm(eq, f)
if f == 0 then
return eq
elseif f == 1 then
return "("..eq:sub(1,3)..")"..eq:sub(4) elseif f == 2 then
return eq:sub(1,4).."("..eq:sub(5)..")"
elseif f == 3 then
return eq:sub(1,2).."("..eq:sub(3,5)..")"..eq:sub(6) elseif f == 4 then
return "("..eq:sub(1,5)..")"..eq:sub(6) elseif f == 5 then
return eq:sub(1,2).."("..eq:sub(3)..")"
elseif f == 6 then
return "(("..eq:sub(1,3)..")"..eq:sub(4,5)..")"..eq:sub(6)
elseif f == 7 then
return eq:sub(1,2).."(("..eq:sub(3,5)..")"..eq:sub(6)..")"
end
end

local Found = {}

for i,v in pairs(Numbers) do
for ii,vv in pairs(Operators) do
local equation = v[1] .. vv[1] .. v[2] .. vv[2] .. v[3] .. vv[3] .. v[4]
for i = 0, 7 do
equation2 = BracketForm(equation, i)
local ans = load("return " .. equation2)()
Found[ans] = ans > 0 and ans%1 == 0 and not Found[ans] and equation2 or nil
end
end
end