You may only use the 4 basic math operations (+, -, *, /) and exponentiation.
Make an expression that results in 1111 using only one digit (chosen from 1 to 9) repeated only
six times and that must be used by itself. For example, if you chose "5", you can't use "55", but an expression like [(5 x 5 x 5 x 5) + 5] / 5 = 126, would work if 126 was the desired result.
Of the 9 choices available just one allows it being used 6 times; any other choice will result in more uses of that digit.
The program is a modification of a program previously used for five 5s and eight 8s, hence the variable name no8s, from the latter. Again, the output uses Reverse Polish Notation; this time there are few results, so translation into algebraic notation was by hand.
DEFLNG A-Z
DECLARE SUB try ()
CLEAR , , 25000
OPEN "1111.txt" FOR OUTPUT AS #2
DIM SHARED no8s, stack#(30), lvl
DIM SHARED StackSize, f$, ctSol, hlvl, digit
FOR digit = 1 TO 9
f$ = STR$(digit): stack#(1) = digit: StackSize = 1: no8s = 1
try
NEXT digit
CLOSE
PRINT ctSol
SUB try
lvl = lvl + 1
IF lvl > hlvl THEN hlvl = lvl: PRINT lvl, f$
IF no8s < 6 THEN
f$ = f$ + STR$(digit)
StackSize = StackSize + 1
stack#(StackSize) = digit
no8s = no8s + 1
try
f$ = LEFT$(f$, LEN(f$) - 2)
StackSize = StackSize - 1
no8s = no8s - 1
END IF
IF StackSize > 1 THEN
h2# = stack#(StackSize)
StackSize = StackSize - 1
h1# = stack#(StackSize)
stack#(StackSize) = h1# + h2#
f$ = f$ + "+"
try
stack#(StackSize) = h1# - h2#
MID$(f$, LEN(f$), 1) = "-"
try
IF h1# = 0 OR h2# = 0 THEN
tst = 1
ELSE
tst = (LOG(ABS(h1#)) + LOG(ABS(h2#))) / LOG(10)
END IF
IF tst < 300 THEN
stack#(StackSize) = 1# * h1# * h2#
MID$(f$, LEN(f$), 1) = "*"
try
END IF
IF h2# <> 0 THEN
stack#(StackSize) = h1# / h2#
MID$(f$, LEN(f$), 1) = "/"
try
END IF
IF h1# = 0 THEN
stack#(StackSize) = 0
MID$(f$, LEN(f$), 1) = "^"
try
ELSE
tst# = LOG(ABS(h1#)) * h2# / LOG(10)
IF ABS(tst#) < 300 AND (h1# > 0 OR h2# = INT(h2#) AND ABS(h2#) < 1000000000000000#) THEN
stack#(StackSize) = h1# ^ h2#
MID$(f$, LEN(f$), 1) = "^"
try
END IF
END IF
stack#(StackSize) = h1#
StackSize = StackSize + 1
stack#(StackSize) = h2#
f$ = LEFT$(f$, LEN(f$) - 1)
ELSE
' IF no8s < 6 THEN
' ELSE
IF stack#(1) > 1110.99999# AND stack#(1) < 1111.00001# THEN
PRINT USING "\ \ ######"; f$; stack#(1)
PRINT #2, USING "\ \ ######"; f$; stack#(1)
END IF
' END IF
END IF
lvl = lvl - 1
EXIT SUB
END SUB
finds the following
6 6 6^+ 6 6 6*+/ 1111
6 6 6^+ 6 6* 6+/ 1111
6 6^ 6+ 6 6 6*+/ 1111
6 6^ 6+ 6 6* 6+/ 1111
Recall that RPN uses a stack, so each new 6 gets pushed onto the stack, and each operation acts on the last two operands on the stack (whether just pushed or the result of a preceding operation), replacing those two with the one result.
The first line represents
6 + 6^6
-------
6 + 6*6
The others merely reverse the addends in either the numerator, the denominator or both.
|
Posted by Charlie
on 2008-08-18 18:39:44 |