Generating a rather intricate loop with inequalities
Ältere Kommentare anzeigen
I have to create a code that, given positive integers m and n, calculates integers k and r such that m=kn+r and r<n by successively subtracting n first from m and then from m-n and then from m-2n and so on in a loop. as many times as possible without the remainder r=m-kn becoming negative, and which finally prints k and r on the screen.
This seems a little confusing, since r and k are not calculated readily, since we have an inequality at r<n, and not an equality. If it was r=n, we would have no problem and could proceed to the loop.
My code is as given, however, it stops since r<n and cannot be calculated. Any ideas how to solve this? Thanks
m=1
n=2
% Initialize the starting number
number = m;
% Loop
while number <= m-n*n
disp(number);
number = number-n; % Subtract n from m
end
if r==m-kn;
end
if r<n;
end
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements 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!