This is in continuation of
Closest to pi.
(A) As we are aware, the approximate value of e with some decimals is 2.7182818284590...
Write an expression using all digits (09) once each, to achieve the closest value to e. The digits 1, 2, 3, 4, 5, 6, 7, 8, 9, and 0 must be arranged strictly in this order. You can only use the five symbols: parentheses , addition, subtraction, multiplication and division symbols that is, (), +, , *, /
Concatenating two or more digits is allowed. Each symbol can occur
at most once in the given expression. Only standard order of operations is followed – that is, multiplication does not take precedence over division, addition does not take precedence over multiplication, and so on.
For example : (123+456)/7890 is a valid expression. However, expressions like 1+234/567 + 8/9 0 or, 1+3*2456/7890 are not allowed.
(B) Write an expression closest to e, if all the other conditions in (A) remain unaltered, but the restriction of each symbol occurring at most one is withdrawn. For example, expressions like 1+234/567 + 8/9 0 is allowed. However, expressions like: 1+3*2456/7890 are not allowed.
(C) What are the respective answers to (A) and (B) if a sixth symbol, that is, the decimal point is allowed?
A very minor tweak on the program used to solve the Close to Pi puzzle. This time run time was also speeded up by use of the QB64 compiler, thanks to brianjn's advocacy of that compiler.
DECLARE SUB addon ()
DEFDBL AZ
DIM SHARED stack(11)
DIM SHARED upTo, stackLevel
DIM SHARED h$, best, usedPlus, usedMinus, usedTimes, usedDiv
best = 9999
stack(1) = 1: stackLevel = 1: upTo = 1
h$ = "1"
OPEN "close2e.txt" FOR OUTPUT AS #2
addon
CLOSE 2
SUB addon
nextOne = upTo + 1
SELECT CASE nextOne
CASE 10
nextOne = 0
CASE 1
' this is the end
' evaluate
IF stackLevel = 1 THEN
diff = ABS(stack(1)  2.718281828#)
IF diff <= best THEN
best = diff
PRINT h$, stack(1)
PRINT #2, h$, stack(1)
END IF
EXIT SUB
ELSE
nextOne = 999
END IF
END SELECT
'first try concatenation
IF nextOne < 999 THEN
lc$ = RIGHT$(h$, 1)
ix = INSTR("0123456789", lc$)
IF ix THEN
h$ = h$ + LTRIM$(STR$(nextOne))
stack(stackLevel) = 10 * stack(stackLevel) + nextOne
utSave = upTo
upTo = nextOne
addon
upTo = utSave
stack(stackLevel) = stack(stackLevel) \ 10
h$ = LEFT$(h$, LEN(h$)  1)
END IF
END IF
'try comma
IF nextOne < 999 THEN
stackLevel = stackLevel + 1
stack(stackLevel) = nextOne
h$ = h$ + "," + LTRIM$(STR$(nextOne))
utSave = upTo
upTo = nextOne
addon
upTo = utSave
stackLevel = stackLevel  1
h$ = LEFT$(h$, LEN(h$)  2)
END IF
GOSUB tryDiadic
EXIT SUB
tryDiadic:
IF stackLevel >= 2 THEN
s1 = stack(stackLevel  1)
s2 = stack(stackLevel)
stackLevel = stackLevel  1
stack(stackLevel) = s1 + s2
h$ = h$ + "+"
IF usedPlus = 0 THEN usedPlus = 1: addon: usedPlus = 0
h$ = LEFT$(h$, LEN(h$)  1)
stack(stackLevel) = s1
stackLevel = stackLevel + 1
stack(stackLevel) = s2
s1 = stack(stackLevel  1)
s2 = stack(stackLevel)
stackLevel = stackLevel  1
stack(stackLevel) = s1  s2
h$ = h$ + ""
IF usedMinus = 0 THEN usedMinus = 1: addon: usedMinus = 0
h$ = LEFT$(h$, LEN(h$)  1)
stack(stackLevel) = s1
stackLevel = stackLevel + 1
stack(stackLevel) = s2
s1 = stack(stackLevel  1)
s2 = stack(stackLevel)
stackLevel = stackLevel  1
stack(stackLevel) = s1 * s2
h$ = h$ + "*"
IF usedTimes = 0 THEN usedTimes = 1: addon: usedTimes = 0
h$ = LEFT$(h$, LEN(h$)  1)
stack(stackLevel) = s1
stackLevel = stackLevel + 1
stack(stackLevel) = s2
s1 = stack(stackLevel  1)
s2 = stack(stackLevel)
IF s2 <> 0 THEN
stackLevel = stackLevel  1
stack(stackLevel) = s1 / s2
h$ = h$ + "/"
IF usedDiv = 0 THEN usedDiv = 1: addon: usedDiv = 0
h$ = LEFT$(h$, LEN(h$)  1)
stack(stackLevel) = s1
stackLevel = stackLevel + 1
stack(stackLevel) = s2
END IF
END IF
RETURN
END SUB
finds the closest value without repeating an operation as:
1,234,56+/,789*,0 2.720689655172414
where the value on the right is obtained by the operations on the left, which are shown in reverse Polish form. Translated into algebraic, that's
1/(234+56)*7890 ~= 2.720689655172414 ,
where, it's to be remembered that multiplication does not take precedence over division, though this notation is ambiguous (see discussion at http://mathforum.org/library/drmath/view/57021.html).
If repetition is allowed:
1,2*,3/,4*,5,6,7,8/+,90+/+ 2.718279569892473
where the RPN translates to 1*2/3*4+5/(6+7/8+90), which I would be more comfortable writing as
(1*2/3)*4+5/(6+7/8+90) ~= 2.718279569892473
to avoid the ambiguity of the relative precedence of multiplication and division.
If we also allow a leading unary negative sign:
1,2/,3,4,5*,6,7+,8/,90+/ 2.718281036834925
The equivalent of the RPN is 1/234*5/((6+7)/8+90), but since this is the approximation of negative, rather than positive, e, we negate to
1/2+3+4*5/((6+7)/8+90) ~= 2.718281036834925.

Posted by Charlie
on 20120418 16:13:17 