Sn*= sum of integers dividable by 11, write a script program that inputs a large positive integer M, computes the smallest integer n∗such that Sn∗> M, then displays n∗and Sn∗.
1 次查看(过去 30 天)
显示 更早的评论
I am honestly so lost with this question, because 1) the wording is awful and 2) I just don't get while loops. The full question is "Let Sn be the sum of integers that are between 1 and n and are dividable by 11. Write a script program that inputs a large positive integer M, computes the smallest integer n∗such that Sn∗> M, then displays n∗and Sn∗." I have this so far.
%Small integer
M=input('Enter number M:');
n=1;
rem(n,11)=0;
while Sn<=M
if rem(n,11)==0
end
n=n+1;
Sn=n;
end
Sns=Sn;
ns=n;
fprintf('ns=%f\n', ns)
fprintf('Sns=%f\n', Sns)
I just don't know what else to put
0 个评论
回答(1 个)
Dyuman Joshi
2022-9-22
编辑:Dyuman Joshi
2022-9-22
You have done quite well despite your limited understanding of the question. Your code requires only a few modifications -
%M=input('Enter number M:');
%random large positive integer
M=123456;
%initiating the sum as 0
Sn=0;
n=1;
while Sn<=M
if rem(n,11)==0
Sn=Sn+n;
end
n=n+1;
end
fprintf('n=%d\n', n)
fprintf('Sn=%d\n', Sn)
We can verify this as well
y=11.*(1:n/11);
sum(y)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!