Find a fourdigit number with four different digits, that is equal to the number formed by its digits in descending order minus the number formed by its digits in ascending order.
(In reply to
My solution by Gamer)
Proof of uniqueness of the solution is obtained by the following program that looks at all numbers formed with four different digits in ascending sequence and subtracting that from it's reverse and sees if the result has the same four digits. It includes the possibility that the leading digit is zero. Only 7641  1467 = 6174 is found by the program.
FOR i = 0 TO 6
FOR j = i + 1 TO 7
FOR k = j + 1 TO 8
FOR l = k + 1 TO 9
diff = l * 1000 + k * 100 + j * 10 + i  (i * 1000 + j * 100 + k * 10 + l)
s$ = LTRIM$(STR$(diff))
IF LEN(s$) < 4 THEN
s$ = RIGHT$("0000" + s$, 4)
END IF
s1$ = LTRIM$(STR$(l * 1000 + k * 100 + j * 10 + i))
DO
ix = INSTR(s1$, LEFT$(s$, 1))
IF ix = 0 THEN EXIT DO
s1$ = LEFT$(s1$, ix  1) + MID$(s1$, ix + 1)
s$ = MID$(s$, 2)
LOOP UNTIL s$ = ""
IF s$ = "" THEN
d = l * 1000 + k * 100 + j * 10 + i
a = i * 1000 + j * 100 + k * 10 + l
PRINT d; ""; a; "="; d  a
END IF
NEXT
NEXT
NEXT
NEXT

Posted by Charlie
on 20030426 09:17:17 