# How can I create an expression that uses numbers 1 - 7 to equal 100?

Sorry to post what is probably a very pedestrian question here (it's actually from my son's homework; he's eight), but it's driving us crazy and I really want to know how I'm meant to approach it.

We need to write an expression that uses the numbers 1, 2, 3, 4, 5, 6 and 7, in that order, to give an answer of 100. We can use multiplication, division, addition and subtraction operators, and brackets.

Is there a technique here or is brute force the correct approach. I'm a software developer and I can't even work out how to write a program to solve this.

-
Could you use other numbers as well or only those numbers? – NoChance Sep 23 '12 at 15:08
Let's say only those numbers. – Edmond Bramhall Sep 23 '12 at 15:41
To write such a program, you just have to make all the legal strings and evaluate them until you find the right answer. As did says, you just have to look. I also don't know any general techniques. – Ross Millikan Sep 23 '12 at 17:17

$$-1\cdot2+3\cdot4\cdot5+6\cdot7$$ There is no general method that I am aware of.

-
Thanks very much. I'm slightly confused by the notation. I think I'd write this as -(1*2)+(3*4*5)+(6*7). Why the unusual character for multiplication, and how are the brackets not needed? Apologies again for the basic question - never really studied any formal maths. – Edmond Bramhall Sep 23 '12 at 15:49
One usually considers that multiplications and divisions are performed before additions and substractions, hence indeed you interpret correctly the formula. The symbol for multiplication may be equivalently $\cdot$ or $\times$ or even $\ast$. – Did Sep 23 '12 at 15:52
Brilliant. Thanks very much! – Edmond Bramhall Sep 23 '12 at 15:55
@EdmondBramhall PEMDAS – seriousdev Sep 23 '12 at 16:17
Just wrote a quick brute force python program. Without using parenthesis to change the order of operations, this is the only correct answer. – Mark Sep 23 '12 at 18:25

From a brute force Python program, here's the list of possible solutions. I apologize for the "mathematical duplicates" caused by the inclusion of parenthesis, but the list was too long to QC.

+1.0-2.0+3.0+(4.0*5.0-6.0)*7.0
+1.0-2.0+3.0+(4.0*(5.0)-6.0)*7.0
+1.0-2.0*3.0+(4.0+5.0+6.0)*7.0
+1.0-2.0*3.0+(4.0+(5.0)+6.0)*7.0
+1.0+(2.0+3.0)*4.0*5.0+6.0-7.0
+1.0+(2.0+3.0)*4.0*5.0+(6.0)-7.0
+1.0+(2.0+3.0)*4.0*(5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6.0*7.0
-(1.0)*2.0+(3.0*4.0)*5.0+(6.0)*7.0
-(1.0)*2.0+(3.0*(4.0)*5.0)+6.0*7.0
-(1.0)*2.0+(3.0)*4.0*5.0+6.0*7.0
-(1.0)*2.0+(3.0)*4.0*5.0+(6.0)*7.0
-(1.0)*2.0+(3.0)*4.0*(5.0)+6.0*7.0


Code used to generate list:

import itertools

signs = ('+','-','/','*','(',')','+(','-(','/(','*(',')+',')-',')/',')*')
nums = range(1,8)

for someSigns in itertools.product(signs,repeat=7):
evalStr = ""
for i, j in zip(someSigns, nums):
evalStr += i + str(float(j))
try:
if eval(evalStr) == 100:
print evalStr
except:
continue

-
Genius - thanks, Mark! – Edmond Bramhall Sep 24 '12 at 9:22

Another way:

$$-1\cdot 2-3+(4+5+6)\cdot 7$$

-
Your answer is questionable in a way. Proper notation is $a(b + c)$ rather than $(b + c)a$ to avoid confusion. Still, have my upvote :-) – Parth Kohli Sep 23 '12 at 14:57
@enzotib: The post is OK now. – Parth Kohli Sep 23 '12 at 15:07
Thanks a lot, Argon! Thanks for correcting my tags as well :-) – Edmond Bramhall Sep 23 '12 at 16:04

If you want a general solution you can just bruteforce it. My naive approach was to print out all possible strings and eval them.

I've put up the source code to do this and like tasks at my Github repository: https://github.com/cairo140/operations

I had to invent a notationally odd superplus and superminus. Basically, x.superplus(y) is equivalent to (x+y) so that 1*2.superplus(3)*4 is 1*(2+3)*4. I believe that between allowing a - to be put ahead of the leading 1 and using superplus and superminus, you can account for all possible permutations ordinarily available through parentheses. I haven't convinced myself of this fact yet, but counterexamples are welcome.

These are all the possible solutions to get 100. Both the examples posted at this time are included in the result set. At the moment, unfortunately there can be redundant solutions.

100=-1+2.superplus(3)*4*5-6+7
100=1+2.superplus(3)*4*5+6-7
100=-1*2+3*4*5+6*7 # did's solution
100=-1+2*3*4+5.superplus(6)*7
100=-1*2-3+4.superplus(5).superplus(6)*7
100=1-2*3+4.superplus(5).superplus(6)*7
100=-1*2.superplus(3)+4.superplus(5).superplus(6)*7 # Argon's solution
100=1*2.superplus(3)*4*5+6/7
100=1*2.superplus(3)*4*5-6/7
100=1+2.superplus(3)*4*5+6.superminus(7)
100=-1+2.superplus(3)*4*5-6.superminus(7)
100=-1*2.superplus(3)*4*5*6.superminus(7)
100=-1*2.superplus(3)*4*5/6.superminus(7)


Some other examples (e.g., at least one way to use 1 2 3 4 5 6 7 to get each number up to 100) are available at the Github repository.

-
Good solution (cleaner code then my hack), but it doesn't take into division and multiplication with parens. – Mark Sep 23 '12 at 20:01

@Edmond Bramhall --want to know how I am meant to approach it.

Hmmm. This is aimed at an 8 year old. Not many 8 year-olds can churn out Python programs. Let's try something a little simpler.

The first step is to get a feel for things. What happens if we add up all the numbers 1+2+...+7? We get 28, which is too small. How do we get a bigger number? Subtracting or dividing will just make it smaller. So we have to multiply. We need it about 4 times bigger (3 x 28 = 84, 4 x 28 = 112). Well that is a bit misleading because the 4 will not be multiplying itself, it is more like 4*24. But let's get on and try 4.

There are two obvious ways of getting multiplication by 4. The first is (1+2+3) 4+5+6+7=42 - too small. [Bother cannot figure out how to turn off the italics!] The other is (1+2+3)+4(5+6+7) = 78. Still too small. So this is slightly trickier than it looks.

If we go to 5*(6+7) it will be too big.

Pause. 6*7=42. And (3*4*5)=60. So that would get us close. To 102 in fact. Could we get the -2 from the 1,2,3? That bit I leave to you.

But you may still complain that I pulled 6*7 and 3*4*5 out of thin air. Well, not completely. My thought process was as follows:

I need something bigger. 6*7=42. Oh, but then 3*4*5=60.

That does not look like a particularly logical progression. It isn't logical, but it is natural. 6,7 just shrieks 42. That is hardwired into my brain. 3,4,5 certainly whispers 60, maybe even speaks it.

There are for me two lessons from this. (1) you need to rote learn things. No progress is possible until your brain is stocked with useful facts. They have to be in your brain. Because you need really fast retrieval. If you have to mess around working out 6*7=42, it is much harder. (2) You need some organised playing around. By far the best books on that are the old Polya ones. An 8 year old could probably cope with his short book.

-

The answer to this question is as follows, 1+2+34+56+7=100. We have now used all of the numbers required to equal 100.

-
this is kinda brilliant, but i'd like to hear if this is within the rules! – saturatedexpo Jul 2 at 1:42