# How many consecutive integers can you make using only four digits?

In the spirit of the classic four fours, I wonder what's the optimal set of four numbers?

Your goal is to make the most consecutive integers using four digits of your choice. Pick four: $0,1,2,3,4,5,6,7,8,9$ ( You can pick multiple instances of the same digit )

When constructing an integer:

• all of your four candidates must be used exactly once (order/placing of digits is irrelevant)
• You may use basic arithmetic operations $+,-,\times,\div$ and parentheses $()$
• You may use $a^b$ and $\sqrt[a]{b}$ but at the expense of 2 numbers as you can see
• You may not form new numbers, i.e. $ab$ is not allowed

If we were to use four $4$s, the best we could do would be up to $9$:

 0  =  4 ÷ 4 × 4 − 4
1  =  4 ÷ 4 + 4 − 4
2  =  4 −(4 + 4)÷ 4
3  = (4 × 4 − 4)÷ 4
4  =  4 + 4 ×(4 − 4)
5  = (4 × 4 + 4)÷ 4
6  = (4 + 4)÷ 4 + 4
7  =  4 + 4 − 4 ÷ 4
8  =  4 ÷ 4 × 4 + 4
9  =  4 ÷ 4 + 4 + 4
*10  =  4 ÷√4 + 4 ×√4
*10  =  (44 − 4) ÷ 4


Number 10 can't be done and is an example of failing, since it would require either:

• expenseless roots; $\sqrt{4}$ isn't allowed. ( $\sqrt{4}$ is, which requires you to use $4$ and $2$ )

• number formation which isn't allowed either.

Zero does not necessarily need to be included, you can start at either $0$ or $1$.

For the purposes of freedom of puzzling, if you think you can top your solution for a chosen set of digits, by starting at any other positive integer, you can add that to your answer below your initial solution. (I suspect this is unlikely)

If you want, you can extend your consecutive list to negative integers but this is strictly optional and not necessary in any way, other than for the purposes of fulfillment and mathematical euphoria.

Example

There is an example on Puzzling.SE using digits $2,2,4,5$:

But this can be expanded since the given example uses only basic arithmetic operations, not including potentiation and roots. I also suspect It could be done better using another set.

I tried this by hand and I'm stuck at number $29$ using this example set, and at number $34$ using $9,8,3,2$.

• @Oray I don't see why it would be against the rules, so why not. – Vepir Apr 22 '17 at 11:23
• Does allowing "$-$" minus at the beginning also mean that any number may be negated? As in $1+1+(-1)+(-1) = 0$ . Either way, it probably should be mentioned in the puzzle statement rather than in comments. Also, either way, it effectively makes more than 4 numbers available for use. – humn Apr 22 '17 at 21:31
• What you call "number formation" is, I believe, called "concatenation" in math. – Fund Monica's Lawsuit Apr 22 '17 at 23:22
• @humn I believe we can avoid "negation"? In your example, you don't need a plus in front of a minus, and that's subtraction then. Also, instead of negating the first number, just move it to the right. Negation of exponents is also unnecessary since it results in exponentiation+division. Can you give me an example where negation can't be avoided if my intuition is wrong? (Other than if we want to form more negative numbers) – Vepir Apr 23 '17 at 8:45
• Good points about rearranging negation, @Vepir. The only contrary examples I can think are quite unlikely to be helpful, such as $3^{-2}+\frac89 = 1$. – humn Apr 23 '17 at 9:10

Here is my result for $1,4,6,7$ from $1$ to $53$.

$1^{4-6-7}=1$

$1^4-6+7=2$

$(1-4)/(6-7)=3$

$1+4+6-7=4$

$1-4/(6-7)=5$

$1+4-6+7=6$

$(1^{4})^{6}*7=7$

$1^{4-6}+7=8$

$-1*(4-6-7)=9$

$1-4+6+7=10$

$1^6*(4+7)=11$

$1^6+4+7=12$

$1^4*(6+7)=13$

$1^4+6+7=14$

$(1-6)*(4-7)=15$

$-1+4+6+7=16$

$1*(4+6)+7=17$

$1+4+6+7=18$

$1-6*(4-7)=19$

$-4*(1^7-6)=20$

$-1+4*7-6=21$

$1*(4*7-6)=22$

$1+4*7-6=23$

$1^7*4*6=24$

$1^7+4*6=25$

$4*(1+7)-6=26$

$-4*(1-6)+7=27$

$1^6*4*7=28$

$1^6+4*7=29$

$-1+4*6+7=30$

$1*(4*6+7)=31$

$1+4*6+7=32$

$-1+4*7+6=33$

$1*(4*7+6)=34$

$1+4*7+6=35$

$(7-1)^{6-4}=36$

$-1-4+6*7=37$

$-1*(4-6*7)=38$

$1-4+6*7=39$

$4-6*(1-7)=40$

$6+7*(4+1)=41$

$1^4*6*7=42$

$1^4+6*7=43$

$-4+6*(1+7)=44$

$-1+4+6*7=45$

$1*(4+6*7)=46$

$1+4+6*7=47$

$-1+7^{6-4}=48$

$1*7^{6-4}=49$

$1+7^{6-4}=50$

$-1+4*(6+7)=51$

$1*4*(6+7)=52$

$1+4*(6+7)=53$

• Do you think this might be the optimal set? – Vepir Apr 22 '17 at 15:22
• @Vepir I believe this is optimal if I do not mess up with my programming. I checked every possibility. – Oray Apr 22 '17 at 15:23
• I'll most likely accept it at the end of the day. What did you used to program it? Would you mind sharing your code on something like pastebin? – Vepir Apr 22 '17 at 15:28
• I accepted your answer since it seems it's unlikely that we get a better result. I would still like to check out your code? :) – Vepir Apr 23 '17 at 18:15
• @Oray people may still be waiting for the code ^^ – Cœur Sep 30 '18 at 18:39

I revisited the question, and ran a brute force over all possible cases, and found that there are two optimal solutions. I used the following github code and modified it a bit to fit my task.

Other than the $$1,4,6,7$$ digit set that holds the optimal record of $$53$$ that Oray found, we can also accomplish the same record with digit case $$2,3,8,9$$:

$$\begin{array}{} 1 = (8 - (2 - 3)) \div 9\\ 2 = 9 - ((2 - 3) + 8)\\ 3 = 9 \div (8 - (2 + 3))\\ 4 = ((2 + 3) + 8) - 9\\ 5 = ((2 \times 3) + 8) - 9\\ 6 = ((2 + 3) - 8) + 9\\ 7 = ((2 \times 3) - 8) + 9\\ 8 = 9 - ((2 - 3) ^ {8})\\ 9 = ((2 - 3) ^ {8}) \times 9\\ 10 = ((2 - 3) ^ {8}) + 9\\ 11 = 9 - ((2 \times 3) - 8)\\ 12 = 9 - ((2 + 3) - 8)\\ 13 = (2 \times (3 + 8)) - 9\\ 14 = ((2 \div 3) \times 9) + 8\\ 15 = (9 - (8 \div 2)) \times 3\\ 16 = ((2 - 3) + 8) + 9\\ 17 = 9 - ((2 - 3) \times 8)\\ 18 = 9 - ((2 - 3) - 8)\\ 19 = 9 - (2 \times (3 - 8))\\ 20 = 8 - (2 \times (3 - 9))\\ 21 = (8 \div (2 \div 3)) + 9\\ 22 = ((2 + 3) + 8) + 9\\ 23 = ((2 \times 3) + 8) + 9\\ 24 = ((8 - 2) ^ {3}) \div 9\\ 25 = ((2 ^ {3}) + 8) + 9\\ 26 = ((3 ^ {2}) + 8) + 9\\ 27 = (8 - (2 + 3)) \times 9\\ 28 = ((2 \times 8) + 3) + 9\\ 29 = 8 - ((2 - 9) \times 3)\\ 30 = ((2 + 8) \div 3) \times 9\\ 31 = ((2 + 3) \times 8) - 9\\ 32 = (9 - (2 + 3)) \times 8\\ 33 = 8 - (2 - (3 \times 9))\\ 34 = ((3 - 8) ^ {2}) + 9\\ 35 = (2 + (3 \times 8)) + 9\\ 36 = (2 - 8) \times (3 - 9)\\ 37 = ((2 + 3) \times 9) - 8\\ 38 = 2 \times ((3 \times 9) - 8)\\ 39 = ((2 \times 3) \times 8) - 9\\ 40 = (2 + (9 \div 3)) \times 8\\ 41 = (2 ^ {8 - 3}) + 9\\ 42 = (2 + (8 \div 3)) \times 9\\ 43 = (2 \times 8) + (3 \times 9)\\ 44 = ((3 - 9) ^ {2}) + 8\\ 45 = (9 - (2 - 8)) \times 3\\ 46 = ((2 \times 3) \times 9) - 8\\ 47 = 2 - ((3 - 8) \times 9)\\ 48 = ((2 \div 3) \times 8) \times 9\\ 49 = ((2 + 3) \times 8) + 9\\ 50 = 2 - ((3 - 9) \times 8)\\ 51 = ((8 - 2) \times 9) - 3\\ 52 = ((8 ^ {2}) - 3) - 9\\ 53 = ((2 + 3) \times 9) + 8\\ \end{array}$$

• Similarly to Oray, you also miss 0, (9+2) - (3+8), leading to a total of 54 – Fifth_H0r5eman Aug 14 '19 at 9:59
• @Fifth_H0r5eman The goal is to reach maximal value, so it is still $53$. The $0$ is optional as stated in the question. Edit: I also haven't considered variations such as where starting number is $\ge 1$, which are suggested in the original question (Where in that case we are counting the maximal streak). – Vepir Aug 14 '19 at 10:01
• It reads in the question as: "Your goal is to make the most consecutive integers", not reach the maximum end value. 0 would bump this up to 54 – Fifth_H0r5eman Aug 14 '19 at 10:19
• @Fifth_H0r5eman You're right, I should've been more specific when I was writing the question. - As it seems, my idea of "streaks of X integers" boiled down to "up to number X in non-negative or positive integers". Excuse me for not writing the question more clearly. – Vepir Aug 14 '19 at 10:30

Here's a list of all numbers from 1 to 43 using 2, 3, 6 and 7. ^ is the power operator.

(6-((3+7)/2))
(6*(3/(2+7)))
(2/((7-3)/6))
((6*3)-(2*7))
((7-(3-6))/2)
(((7-6)*2)*3)
(7*((3*2)/6))
(2^(3*(7-6)))
((3^(7-6))^2)
((3^2)+(7-6))
(((7+3)/2)+6)
(((7+6)+2)-3)
(((6/2)+7)+3)
(7+(6-(2-3)))
(((6*2)-7)*3)
(7-(3-(6*2)))
((6-3)+(7*2))
((3+(2+6))+7)
(((3*2)+6)+7)
(2*((7-3)+6))
(6-((2-7)*3))
(7+((2*6)+3))
((6+3)+(2*7))
((6/(2/7))+3)
((7*3)+(6-2))
(6+((3+7)*2))
((2+7)*(6-3))
((2/(3/7))*6)
(6+((3*7)+2))
(2*((7*3)-6))
(7+((6+2)*3))
(3-(7-(6^2)))
(3*((7-2)+6))
(((6/2)^3)+7)
(((6-3)+2)*7)
(6*(2-(3-7)))
((6*(3+2))+7)
((7-(2/3))*6)
((3+(7/2))*6)
((6^2)-(3-7))
((7*6)-(3-2))
(((3-2)*6)*7)
(3-(2-(6*7)))

As you can guess, I wrote a program to find this. It's not so hard to make one, but I'm disclosing it anyway. It puts the output into spoiler code blocks automatically ;)

The program below doesn't use the root operator because most of the time it's useless. If you want to include it, add 'r' to the operators array, but beware of overflow errors.

I stole the RPN parser from here.

import random

operators = ['-', '+', '*', '/', '^']

def parse(rpn):

stack = []

for val in rpn:
if val in operators:
if len(stack) < 2:
return -1
op1 = stack.pop()
op2 = stack.pop()
if val=='-': result = op2 - op1
if val=='+': result = op2 + op1
if val=='*': result = op2 * op1
if val=='/':
if op1 != 0:
result = op2 / op1
else:
return -1
if val=='^':
if op2 > 0 and op1 < 6:
result = op2 ** op1
else:
return -1
if val=='r':
if op2 > 0 and op1 != 0:
result = op2 ** (1/op1)
else:
return -1

stack.append(result)
else:
stack.append(float(val))

if len(stack)!= 1:
return -1
return stack.pop()

def randrpn(digits):
rpn = digits[:]
rpn.append(random.choice(operators))
rpn.append(random.choice(operators))
rpn.append(random.choice(operators))
random.shuffle(rpn)
return rpn

def randdigits():
d = []
d.append(random.choice(range(10)))
d.append(random.choice(range(10)))
d.append(random.choice(range(10)))
d.append(random.choice(range(10)))
return d

def rpn_to_infix(rpn):
stack = []
infix = ""
for nxt in rpn:
if nxt in operators:
op1 = stack.pop()
op2 = stack.pop()
expr = '(' + op2 + nxt + op1 + ')'
stack.append(expr)
else:
stack.append(str(nxt))
return '>! ' + stack.pop() + '<br>'

d = randdigits()
results = []
formulas = []
for i in range(300000):
r = randrpn(d)
res = parse(r)
if (res >= 0 and res.is_integer() and not res in results):
results.append(res)
formulas.append(rpn_to_infix(r))

for i in range(1, 1000):
if float(i) in results:
print formulas[results.index(float(i))]
else:
break

• I suppose you could write a script to translate it into standard notation for readability. I would appreciate it, since I'm doing my own set of digits by hand atm. – Vepir Apr 22 '17 at 12:34
• @Vepir Infix converter done. – BaSzAt Apr 22 '17 at 13:16
• Why picking randomly in randdigits when there are only 10^4 = 10000 possibilities to brute force it? – Cœur Sep 30 '18 at 18:37