remainder while dividing a number by x number of digits
2 次查看(过去 30 天)
显示 更早的评论
if i have a line of code like this
while(rem(i,2) ~= 0 || rem(i,3) ~= 0 || rem(i,4) ~= 0
its OK if I know the limit of 'i' , what can I do to make i open ended such that I can input 'i' as any number and the while loop will be performed . E.G i = 30 , the the while loop continues up to 'rem(1,4)~=0'
3 个评论
Guillaume
2016-6-23
Olubukola, bars, |, in posts have special meaning (they format text like this) Therefore, even though you wrote them they did not appear.
If you format your code as code by pressing the {}Code button above the post, then they'll show up as intended.
采纳的回答
Guillaume
2016-6-23
编辑:Guillaume
2016-6-23
Avoid using i as a variable name, it's meaningless and it's also a function in matlab.
numdigits = 30; %your limit
value = 150; %your i
%test value against all numbers between 1 all at once (use array 1:numdigits)
%if ANY of the remainder is not 0, do something
while any(rem(value, 2:numdigits))
%do something
end
2 个评论
Guillaume
2016-6-23
编辑:Guillaume
2016-6-23
value is the variable you've called i, I just gave it a better name.
Your code is equivalent to:
while any(rem(i, 2:20))
i = i + 20;
rem(i, 2:20) will result in a vector of the remainders of i divided by the integers 2 to 20. any on that vector will be true if any of then is not zero.
But please, use a better variable name than i.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!