Where is the issue in my "while" statement?
6 views (last 30 days)
I want to evaluate the first integer evenly divisible by 3 and 5 using a Matlab code. My attempt is the following:
while mod(n,5)~=0 && mod(n,3)~=0
I'm new to Matlab and wondering where the issue here is. The code returns n=3 for some reason. I also want to extend this to find the first number evenly divisible by 1-10, for which I wrote the following while loop which also did not return the expected value. Where is my error?
the cyclist on 29 Jul 2017
Edited: the cyclist on 29 Jul 2017
In your second case, you are doing
and so on, because you are always incrementing n right along with k. Those mod functions are always equal to zero, so they always satisfy the while condition, so you get to n = 10.
Teja Muppirala on 30 Jul 2017
You were on the right track. This gives 2520:
while any( mod(n,1:10)~=0 )
When your conditional has more than one element, all of them must be true for it to evaluate as true. This will return 'B' and 'C'.
if [1 1 1 0]
if [1 1 1 1]