Friday, 14 June 2013

Expressing Numbers with Number Puzzle


We are given four integers 1, 2, 3 and 4 to express other numbers with an arithmetic expression using these numbers exactly once and the mathematical operators +,-,* (addition, subtraction, multiplication).  For example 1 can be represented as 1 = (2*3)-(4+1). The arithmetic operation can be used zero or multiple times but the digits(1, 2, 3, 4) should be used exactly once. So an expression to represent 1 = 2+3-4 is not valid. Where as 10 = 1+2+3+4 is a valid expression to represent 10. Also you are allowed to reuse operators (such as 24 = 1*2*3*4) and you are allowed to put '(', ')' at any desired position to change the precedence of evaluation.  But you are not allowed to join digits together such as (12+34).



Now the question is:
  1. Using such expression, what is the smallest positive integer that can not be represented by digits 1, 2, 3, 4 and arithmetic operators(+,-,*).
  2. The second question is to find a set of 4 distinct positive integers a,b,c,d such that the smallest positive integer that can not be represented by such expressions involving a,b,c,d (instead of 1,2,3,4) is greater than the answer to part 1. For example if lets say the answer to 1 is number 11, then we have to find a,b,c and d such that all the numbers till 1 to 11 can be represented by expression consisting of a,b,c and d and operators +,-,*
  3. Can you give a computer program that can solve problem 1 and problem 2.

Scroll Down for answer
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

The answer to question 1 is 29, as all the number till 29 can be expressed using 1,2,3,4 and+,-,*.
Here is the list. Please note that there can be multiple ways to represent the same number..
1:(2*3)-(4+1)
2:(4-1)+2-3
3:(4*1)-3+2
4:(4*1)*(3-2)
5:(4*1)+3-2
5:(((4*1)*2)-3)
6:(((4*1)-2)*3)
7:(((4+1)*2)-3)
8:(((4-1)+2)+3)
9:(((4*1)+2)+3)
10:(((4+1)+2)+3)
11:(((4*1)*2)+3)
12:(((4-1)+3)*2)
13:(((4+1)*2)+3)
14:(((4*1)*3)+2)
15:(((4-1)+2)*3)
16:(((4+1)+3)*2)
17:(((4+1)*3)+2)
18:(((4*1)+2)*3)
19:(((4+2)*3)+1)
20:(((3*1)+2)*4)
21:(((4+1)+2)*3)
22:(((4*3)-1)*2)
23:(((4*3)*2)-1)
24:(((4*1)*2)*3)
25:(((4*3)*2)+1)
26:(((4*3)+1)*2)
27:(((4*2)+1)*3)
28:(((3*2)+1)*4)
29:<NoSolution>

Now for question two there are multiple solution possible. For example  <1,2,4,5> or <1,2,5,6>. To prove that following is the list for <1,2,4,5>.
1:(((5-4)*2)-1)
2:(((5-1)+2)-4)
3:(((5*1)+2)-4)
4:(((5-1)*2)-4)
5:(((5*2)-1)-4)
6:(((5*1)*2)-4)
7:(((5*1)-2)+4)
8:(((5-1)-2)*4)
9:(((4-2)*5)-1)
10:(((5-1)+2)+4)
11:(((5*1)+2)+4)
12:(((5*1)-2)*4)
13:(((5*2)-1)+4)
14:(((5*1)*2)+4)
15:(((5*2)+1)+4)
16:(((5+1)*2)+4)
17:(((5*4)-1)-2)
18:(((5*1)*4)-2)
19:(((5*4)+1)-2)
20:(((5+1)+4)*2)
21:(((5*4)-1)+2)
22:(((5*1)*4)+2)
23:(((5*4)+1)+2)
24:(((5-1)+2)*4)
25:(((4-1)+2)*5)
26:(((5+1)*4)+2)
27:(((5+2)*4)-1)
28:(((5*1)+2)*4)
29:(((5+2)*4)+1)
30:(((4-1)*5)*2)
31:(((4+2)*5)+1)
32:(((5-1)*2)*4)
33:<No Solution>
Leaving the combination <1,2,5,6> to the readers to find the answer.

Now coming to question 3. Providing a C++ program code that searches all the combination possible by getting permutation of numbers and then form that permutation tries out all the possible expressions. If a positive result it found then push it to a map. At the end of program it prints out all the numbers possible using given integers. The source code is given for 1,2,3 and 4. Simply change the numbers in the main function to try out for any other combination.
Here is the source code link: expressionPuzzle.cpp
Hope you like this puzzle!!

No comments:

Post a Comment