Finding a number with conditions while using for loop
126 次查看(过去 30 天)
显示 更早的评论
Write a program in a script file that finds the smallest odd integer that is divisible by 13 and whose square root is greater than 120. Use a for-loop in the program. The loop should start from 1 and stop when the number is found. The program prints the message “The required number is:” and then prints the number. Hint: Use rem(n,2)~=0 for finding odd integers, and rem(n,13)==0 for finding the number to be divisible by 13.
I know this is easily solved through other methods but I don't know what to do.
I've rewritten my code many times and don't know what I am doing.
采纳的回答
Image Analyst
2020-4-2
编辑:Image Analyst
2020-4-2
You need a continue. And you should print i, NOT n. Then, after the fprintf() put a break
if sqrt(i) < 120
continue; % Skip to end of loop and continue iterating when sqrt(i) < 120
end
fprintf('The required number is: %d.\n', i);
break; % Break out of loop.
And you could just have n be some huge number, like 100 billion or whatever.
5 个评论
Image Analyst
2020-4-2
No, it's not. Never change the loop iterator or it's starting and ending values in the for loop body. You won't get the expected results. For example
n = 9;
for k = 1 : n
fprintf('Iteration %d, n = %d.\n', k, n);
n=n+1;
end
gives
Iteration 1, n = 9.
Iteration 2, n = 10.
Iteration 3, n = 11.
Iteration 4, n = 12.
Iteration 5, n = 13.
Iteration 6, n = 14.
Iteration 7, n = 15.
Iteration 8, n = 16.
Iteration 9, n = 17.
You can see that you still do iterations 1 through 9 despite the fact that you unwisely changed the ending value inside the loop from 9 to (eventually) 17. You got 9 iterations, not 17.
Here is how I'd do it:
fprintf('Beginning to run %s.m ...\n', mfilename);
for k = 1 : 100000
if sqrt(k) < 120 || rem(k, 13) ~= 0
continue; % Skip to end of loop and continue iterating when sqrt(i) < 120
end
% If you get to here, you've found it!
fprintf('The required number is: %d.\n', k);
break; % Break out of loop.
end
fprintf('Done running %s.m.\n', mfilename);
更多回答(2 个)
John D'Errico
2020-4-2
编辑:John D'Errico
2020-4-2
Why use a loop? ;-)
Lets see, the smallest INTEGER with the desired property is 14404. We can get that as:
n = 120;
>> m = n^2 + mod(-n^2,13)
m =
14404
>> sqrt(m)
ans =
120.01666550942
>> rem(m,13)
ans =
0
But, then I see that you needed to find the smallest ODD integer, and since the smallest such integer is even, we need to find a solution yielding the smallest odd integer. This will work, but it is sort of a kludge:
n = 120;
m = n^2 + mod(-n^2,13);
if rem(m,2) == 0
m = m + 13;
end
m
m =
14417
Well, yes. This is a homework problem.
Hmm. How would I solve it using a loop? After all, you are making a credible effort.
The smallest number that satisfies the listed conditions MUST be one of the integers in the set: [120^2 + (0:(2*13-1))]. So we never need to loop over more than 26 elements beyond 120^2. THINK ABOUT IT! As such, I could set this up as a for loop, over 26 numbers, then breaking out of the loop when we find success.
Or, you could just use a while loop, which requires far less thought.
n = 120;
m = n^2;
while ~isequal(mod(m,[13,2]),[0 1])
m = m + 1;
end
m =
14417
You should see the isequal test reduces two tests into one vectorized test. As well, since we started out at 120^2, we absolutely know that sqrt(m) must be greater than 120.
0 个评论
baiel kurstanbek
2021-12-21
Write a program in a script file that finds the smallest even integer that is divisible by a and by b whose square root is greater than c. Use a loop in the program. The loop should start from 1 and stop when the number is found.
Sample Input: 3
5
7
Sample Output: 60
1 个评论
Image Analyst
2021-12-21
编辑:Image Analyst
2021-12-21
@baiel kurstanbek Read this link:
Hint: Look up the functions sqrt(), rem(), mod(), gcd(), break, for, and while. Start a new question (not part of @Bryan Cordeiro's question) with your attempt at code if you still have questions.
Usually divisible means that the ratio of the two numbers is an integer like
output = 60/3
however it looks like you've either made a typo (like 60 should have been 70), or you're allowing absolutely anything since 60 is not an integer multiple of 7
output = 60/7
So in that case (allowing results with fractional parts), any number is divisible by any other number and you don't need to check for divisibility.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!