Generating a rather intricate loop with inequalities
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!