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.

 Akzeptierte Antwort

Star Strider
Star Strider am 11 Sep. 2015

2 Stimmen

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.

2 Kommentare

Marjorie Sanford
Marjorie Sanford am 11 Sep. 2015
Thank you so much for your help!
Star Strider
Star Strider am 11 Sep. 2015
My pleasure!
If you get stuck, it would be best if you post your code so we can see where the problem is. (I have a correctly-working solution.)
If my Answer helped you solve your problem, please Accept it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Joseph Cheng
Joseph Cheng am 10 Sep. 2015
Bearbeitet: Joseph Cheng am 10 Sep. 2015

1 Stimme

Where are you getting stuck? This is a simple linear algebra question (Ax=B) hint use mldivide() or the shortcut \. You have 6 unknowns and 6 conditions.

1 Kommentar

Marjorie Sanford
Marjorie Sanford am 10 Sep. 2015
My teacher gave me this assignment with out any help or guidelines. I was just introduced to matlab yesterday and I don't know the first thing about it. I spent two hours trying to figure it out today and I can't get anywhere. I understand the algebra but not how to code it into matlab.

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 11 Sep. 2015

1 Stimme

Marjorie, I get the feeling that some of the other answers are too complicated for you. If you want to do a not-too-clever, but dumb, brute force test, you can do this:
% 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.
for d1 = 0:9
for d2=0:9
for d3=0:9
for d4=0:9
for d5 = 0:9
for d6=0:9
c1 = d1==d3+1;
c2 = d2 == d4-1;
% and so on for the c3-c6 conditions.
% Now check if all conditions are true
if c1&c2&c3&c4&c5&c6
finalNumber = d1*100000 + d2*10000+...
break
end
end
end
end
end
end
end
I've left some things for you to finish there. It's really a straightforward but not too clever or efficient way but it's easy to understand and should run in much less than a second.

2 Kommentare

So I can't quite get this program to work. I really tried to figure it out by myself but I need help.
%Mensa_Challenge.m %solve the problem
clear all; close all; clc
for d1=0:9 for d2=0:9 for d3=0:9 for d4=0:9 for d5=0:9 for d6=0:9 for d7=30
c1 = d1==d3+1;
c2 = d2 == d4-1;
c5 = d5 == d3-1;
c6 = d6 == d4+1;
c3 = d3 == d1-1;
c4 = d4 == d2+1;
c7 = d7 == d1+d2+d3+d4+d5+d6
if c1&c2&c3&c4&c5&c6
finalNumber = d1*100000 + d2*100000 + d3*100000
+ d4*100000 + d5*100000 + d6*100000
break
end
end
end
end
end
end
end
end
Image Analyst
Image Analyst am 14 Sep. 2015
Think: what does each digit represent? Do you really want to multiply each digit by one hundred thousand? In the number 724681, does the 8 represent 100,000 (like you have it) or does it represent the 10's place and should be multiplied by 10 instead of 100,000?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Number games finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by