MATLAB: How to find this six digit number

brain teaserdoit4memensano attemptpuzzle

Find a six digit number in which the first digit is one more than the third, the second digit is one less than the fourth, the fifth digit is one less than the third, and the sixth digit is one more than the fourth. The sum of the second and third digits equals the first. The sum of all digits is thirty. This is a Mensa Challenge Mat-lab deliverable.

Best Answer

That seems to me to be a bit of a strenuous start to a MATLAB course!
If you can do the algebra, then all you need to know is how to code it. Consider the first condition, ‘the first digit is one more than the third. That becomes d1=d3+1. You have to put all the digit coefficients on the left hand side, and all the constants on the right, so you would code this as d1-d3=1:
D(1,:) = [1 0 -1 0 0 0] % First Row Of ‘D’ Matrix
K(1,:) = [1] % First Row Of ‘Constant’ Matrix
The others work the same way. The last row of ‘D’ would be [1 1 1 1 1 1] and the corresponding ‘K’ would be 30. Then if you define ‘N’ as [d1; d2; d3; d4; d5; d6] you have to solve D*N=K for ‘N’. I leave that to you.