remainder while dividing a number by x number of digits
4 ビュー (過去 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!