# messup: only 190

November 16 2005 at 6:10 PM

Response to Found 191

David's code is great, but I don't understand it at all. I typed up my own code in two versions. First I made a random finder that found 81, then I devised a systematic way to get all of the answers. Here's the kicker:

The program only yielded 188 answers.
I ran through and compared equations, and realized that I had fallen to the almighty "roundoff error". I fixed the bug, but the code still only gave me 190, not 191. Either I was missing one or David had an extra.

I reformatted my code to output in the same order as his, and I found out that David's 189th equation wasn't on my list. Sorry Dave, but

1 / 2 / 3 + 4 - 5 * 6 + 7 + 8 - 9 = -1, not 1

anyway, so there are only 190 solutions, not 191. Here's my program if you want a peak, I made it as friendly as possible. Notice the loading bar. Non-friendly version: 52 lines long.

DEFINT A-Z 'setup of program
OPEN "c:/solution1.txt" FOR OUTPUT AS #1 'open file for output
CLS
start\$ = "1 2 3 4 5 6 7 8 9" 'set equation template
DIM j\$(5)
j\$(1) = "+" 'these are the five possiblities
j\$(2) = "-" 'for between numbers" nothing,
j\$(3) = "." 'and the 4 operators.
j\$(4) = "*"
j\$(5) = "/"

'test every possibility
FOR s = 1 TO 5: FOR t = 1 TO 5: FOR u = 1 TO 5: FOR v = 1 TO 5
FOR w = 1 TO 5: FOR x = 1 TO 5: FOR y = 1 TO 5: FOR z = 1 TO 5

IF s = 3 AND t = 3 THEN GOTO nexttest 'no three digit numbers
IF t = 3 AND u = 3 THEN GOTO nexttest
IF u = 3 AND v = 3 THEN GOTO nexttest
IF v = 3 AND w = 3 THEN GOTO nexttest
IF w = 3 AND x = 3 THEN GOTO nexttest
IF x = 3 AND y = 3 THEN GOTO nexttest
IF y = 3 AND z = 3 THEN GOTO nexttest

FOR q = 1 TO 5: IF q = 3 THEN q = q + 1
IF s <> q AND t <> q AND u <> q AND v <> q THEN 'must include all four
IF w <> q AND x <> q AND y <> q AND z <> q THEN 'operators:+-*/
GOTO nexttest
END IF
END IF
NEXT q
'ok, the equation is
'valid. Build it.
'put all the operators together
a\$ = j\$(s) + j\$(t) + j\$(u) + j\$(v) + j\$(w) + j\$(x) + j\$(y) + j\$(z)

'put the numbers and operators together
dat\$ = start\$
FOR q = 1 TO 8: MID\$(dat\$, q * 2, 1) = MID\$(a\$, q, 1): NEXT q

'get rid of the dots.
a\$ = ""
FOR q = 1 TO 17
IF MID\$(dat\$, q, 1) <> "." THEN a\$ = a\$ + MID\$(dat\$, q, 1)
NEXT q
dat\$ = a\$

'solve it

solve! = VAL(dat\$)
FOR r = 1 TO LEN(a\$) - 9
dat\$ = MID\$(dat\$, LEN(STR\$(VAL(dat\$)))) 'next number
b = VAL(MID\$(dat\$, 2))
SELECT CASE MID\$(dat\$, 1, 1) 'look at sign
CASE "+": solve! = solve! + b
CASE "-": solve! = solve! - b
CASE "/": solve! = solve! / b
CASE "*": solve! = solve! * b
END SELECT
dat\$ = MID\$(dat\$, 2) 'next sign
NEXT r
solve! = INT(solve! * 10000) / 10000 'fix comp error

IF solve! <> 1 THEN GOTO nexttest

sols = sols + 1
LOCATE 4, 3: PRINT sols
LOCATE 5, 3: PRINT a\$ + " "
PRINT #1, a\$

nexttest:
key\$ = INKEY\$: IF key\$ <> "" THEN END 'check for exit
NEXT z: NEXT y: NEXT x: NEXT w 'next
lb! = lb! + .1 'update loadbar
LOCATE 3, 1
PRINT STRING\$(INT(lb!), "_*")
NEXT v: NEXT u: NEXT t: NEXT s 'next

cheers!

 Respond to this message
Responses