I am trying to find all the multiples of 3 below 1000. I've tried using mod but im a bit stuck.

16 views (last 30 days)
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

Answers (2)

Geoff Hayes
Geoff Hayes on 7 Mar 2019
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 Comments
Geoff Hayes
Geoff Hayes on 7 Mar 2019
Edited: Geoff Hayes on 7 Mar 2019
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?
Andrew Mackintosh
Andrew Mackintosh on 7 Mar 2019
I was trying to break it down into parts so i could understand the steps I was taking.
the overall question is - "find the sum of all the mutples of 3 and 5 below 1000".
I was thinking how do i get to all the multiples of 3 below 1000. then i would figure out how to sum them. then i would repeat that step for the 5's. then add them together.
i was asking the question in a way that would hopefully help me ask less questions in the future.

Sign in to comment.


Steven Lord
Steven Lord on 7 Mar 2019
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 Comments
Andrew Mackintosh
Andrew Mackintosh on 7 Mar 2019
so i got 158 if i had it for a number below 25. 108 for the 3's and 50 for the 5's. if you do it to include 25 then its 185.
I think i have solved it now though as i changed it from 1000 as the limit to 999 as that is the highest number below 1000. still doesnt match the required answer tho but I have checked it on excel doing it manually and get the same answer.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!