MATLAB: Run a program to display the least possible integer divisible by 11 and its square root greater than 132

conditional statementsintegerMATLABwhile loop

clear all
clc
x=1;
while x
if rem(x,11)==0 && x^2>132
disp(x)
end
end
What is wrong with this code?Am I missing something………?

Best Answer

While Jose is correct, the answer is trivial, even with a loop. What is the requirement? The requirement is that
SQRT(x) > 132
You wrote
x^2 > 132
There is a BIG difference. :) Simply making that change in your code will fix the problem.
Hint: You can solve it far more easily though. Rather than checking if rem(x,11)==0, why not change things around? If you ONLY look at multiples of 11, you know that ALL multiples of 11 are divisible by 11. (Sort of a truism.) The advantage is then you never need to check if rem(x,11)==0. You know that to be true in advance.
Since you really did solve the problem already except for that minor inversion...
x = 11;
while sqrt(x) <= 132
x = x + 11;
end
As you can see, x covers all multiples of 11, stopping as soon as sqrt(x) exceeds 132.
Can you do better yet? Of course, if you think carefully about the requirements. If you know that sqrt(x) > 132, then since we know that 132 is divisible by 11, then 132^2 is also divisible by 11. But you want the smallest integer that is divisible by 11 and the sqrt is STRICTLY GREATER than 132. So just add 11.
x = 132^2 + 11
x =
17435
I'm not sure that this final result is what your instructor wants to see though.