Math riddle: What is the smallest positive possible outcome? I spent hours trying to solve this riddle, I got $412-385=27$ as the smallest outcome, does anyone have a way to find the smallest positive outcome and prove it?
The riddle:
You have the digits $1, 2, 3, 4, 5, 8.$
You need to place them in these squares without repeating the same digit twice so that you will get
the smallest positive possible outcome:

 A: If you want a rigorous method, check this. By inserting the digits $x_1, x_2, x_3$ in the top and $x_4, x_5, x_6$ in the bottom, and taking the difference, the result is
$$d =  100 (x_1 - x_4) + 10 (x_2 - x_5) + (x_3 - x_6)$$
Since we want $d >0$, we must have $x_1 - x_4 > 0$. To have it as small as possible, we need $x_1 - x_4 = 1$. We do not need to decide them atm. Now, for $x_2 - x_5$, we have no restrictions, since it is not possible to make $d$ negative if $100(x_1 - x_4) > 0.$ Hence, we need to make $x_2 - x_5$ as small as possible, which clearly is done by setting $x_2 = 1$ and $x_5 = 8$. Similarly for $x_3 - x_6$, but we can not choose 1 or 8, so the best option is $x_3 = 2$ and $x_6=5$. Now we could choose $x_1=4$ and $x_4 = 3$ and we get
$$d = 412 - 385 = 27$$
In case of your python code, its much simpler to use the formula above for $d$. Something like this (pseudo):
for p in Permutations([1,2,3,4,5,8], size=6):

    d = 100(p[0] - p[1]) + .....
    if (d>0)....

A: Hi I solved the question using python, Here is the code:
from itertools import permutations

combinations1 = list(permutations('123458', 3))
lst = []
min1 = 731
flag = True
for i in range(len(combinations1)):
    for j in range(len(combinations1)):
        for k in combinations1[i]:
            if k in combinations1[j]:
                break;
        else:
            res = int(''.join(combinations1[i])) - int(''.join(combinations1[j]))
            if res > 0 and res < min1:
                min1 = res
print(min1)

A: Step 1:In the hundreds position of both numbers, the difference should be 1, since it is the smallest. So you put 2 (above) and 1 (below), or 3 and 2, or 4 and 3, or 5 and 4, depending on the next step.
Step 2:The next two positions in both numbers (tens and units) should be: above the smallest number , below the largest number. The smallest is 12 and the largest is 85, so above we have 12 and below 85. We have 4 and 3 still available, so above 4 and below 3.
The smallest difference is indeed 412-385=27
