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 (0-9) 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/78-90 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/78-90 are not allowed.
(C) What are the respective answers to (A) and (B) if a sixth symbol, that is, the decimal point is allowed?
The below version of the program for close to e, which has been running for over two days and is still running although having been compiled in QB64, has so far already produced a much better approximation to e than the version without decimal points.
The new evaluation is 1/((2+3*.4)/(5/6+7/.890)) ~= 2.71828183520599.
DECLARE SUB addOn ()
DEFDBL A-Z
DIM SHARED stack(11)
DIM SHARED upTo, stackLevel
DIM SHARED h$, best, declevel
best = 9999
stack(1) = 1: stackLevel = 1: upTo = 1: declevel = 0
h$ = "1"
OPEN "close2ep.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
saveDecLevel = declevel
h$ = h$ + LTRIM$(STR$(nextOne))
IF declevel = 0 THEN
stack(stackLevel) = 10 * stack(stackLevel) + nextOne
ELSE
declevel = declevel + 1
stack(stackLevel) = stack(stackLevel) + nextOne / 10 ^ declevel
END IF
utSave = upTo
upTo = nextOne
addOn
declevel = saveDecLevel
upTo = utSave
IF declevel = 0 THEN
stack(stackLevel) = stack(stackLevel) \ 10
ELSE
stack(stackLevel) = stack(stackLevel) - nextOne / 10 ^ declevel
END IF
h$ = LEFT$(h$, LEN(h$) - 1)
IF declevel = 0 THEN
declevel = 1
h$ = h$ + "." + LTRIM$(STR$(nextOne))
stack(stackLevel) = stack(stackLevel) + nextOne / 10 ^ declevel
h$ = LEFT$(h$, LEN(h$) - 2)
utSave = upTo
upTo = nextOne
addOn
declevel = saveDecLevel
upTo = utSave
stack(stackLevel) = stack(stackLevel) - nextOne / 10
END IF
END IF
END IF
'try comma
IF nextOne < 999 THEN
saveDecLevel = declevel
stackLevel = stackLevel + 1
stack(stackLevel) = nextOne
h$ = h$ + "," + LTRIM$(STR$(nextOne))
utSave = upTo
upTo = nextOne
nextOneSave = nextOne
declevel = 0
addOn
nextOne = nextOneSave
upTo = utSave
stackLevel = stackLevel - 1
h$ = LEFT$(h$, LEN(h$) - 2)
stackLevel = stackLevel + 1
declevel = 1
stack(stackLevel) = nextOne / 10
h$ = h$ + ",." + LTRIM$(STR$(nextOne))
utSave = upTo
upTo = nextOne
nextOneSave = nextOne
addOn
nextOne = nextOneSave
upTo = utSave
stackLevel = stackLevel - 1
h$ = LEFT$(h$, LEN(h$) - 3)
declevel = saveDecLevel
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$ + "+"
saveDecLevel = declevel: declevel = 0: addOn: declevel = saveDecLevel
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$ + "-"
saveDecLevel = declevel: declevel = 0: addOn: declevel = saveDecLevel
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$ + "*"
saveDecLevel = declevel: declevel = 0: addOn: declevel = saveDecLevel
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$ + "/"
saveDecLevel = declevel: declevel = 0: addOn: declevel = saveDecLevel
h$ = LEFT$(h$, LEN(h$) - 1)
stack(stackLevel) = s1
stackLevel = stackLevel + 1
stack(stackLevel) = s2
END IF
END IF
RETURN
END SUB
The RPN output that was translated at the top of this comment is:
1,2,3,.4*+,5,6/,7,.890/+// 2.718281835205992
|
Posted by Charlie
on 2012-04-21 01:11:00 |