I am trying to find all the multiples of 3 below 1000. I've tried using mod but im a bit stuck.
16 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to find all the multiples of 3 below 1000. Ive tried using mod but all i am getting is Local 0 and i dont really know what i am doing. I have looked up the guide to using mod but i dont know where i am going wrong.
Thanks
Andy
0 个评论
回答(2 个)
Geoff Hayes
2019-3-7
Andrew - if you have a number n then it is divisible by three if the remainder after division by three is zero (see mod for details). So in your case
n = 42;
if mod(n,3) == 0
% then n is divisible by three
end
10 个评论
Geoff Hayes
2019-3-7
编辑:Geoff Hayes
2019-3-7
Andrew - I realize that the other link requires that you sum the numbers which are multiples of three....but your assignment does not. From what I understand, your homework is to use a loop and mod and determine which numbers from 1 to 1000 are divisible by three. So please disregard the code that you have seen in the above link as I believe it will just confuse you.
You have commented above that what i am ultimately trying to do is "find the sum of all the mutples of 3 and 5 below 1000". Is this true? Because your original statement was I am trying to find all the multiples of 3 below 1000. Which problem are you trying to solve?
Steven Lord
2019-3-7
Take a step back for a second. If I told you to solve this problem with just your brain and perhaps a piece of paper and a pencil, can you explain how you're going to do it? What steps are you going to follow? [You might want to do this for a smaller number than 1000 -- let's say go up to 25. The answer in that case is 168, though I suspect your first attempt may give 183.]
Once you know the steps that you would follow, return to MATLAB. Open the Editor and write each of those steps as a comment in a blank file. Then write the MATLAB code to tell MATLAB to perform each step below the comment listing that step. If you're not sure how to perform a step in MATLAB (even though you'd be able to do so yourself) try breaking it up into substeps.
If you reach a point where you're not sure how to implement a step, show us your commented code and we may be able to offer some suggestions.
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!