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∗.

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 nsuch that Sn> M, then displays nand 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

Answers (1)

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)
n=1651
fprintf('Sn=%d\n', Sn)
Sn=124575
We can verify this as well
y=11.*(1:n/11);
sum(y)
ans = 124575

6 Comments

Thank you so much, this really helped. Only one thing is wrong with mine tho, which is an error message that keeps popping up for the remainder line. Do you know why it keeps happening?
What's the full and exact text of the error message? Can you show us all the text displayed in red in the Command Window when the error occurs?
Index in position 1 exceeds array bounds. Index must not
exceed 1.
Error in HW4B1 (line 5)
if rem(n,11)==0
Do you have a function called rem.m in your working directory ? Then rename it.
I don't, sorry. I googled it and it said something about having a variable in the code being displayed but not computed, or something like that.
rem is treated as an array in your code, not as a function. Thus somewhere you must have created an array called "rem".
E.g. the line
rem(n,11)=0;
(which makes no sense) would do this.
If you just had copied @Dyuman Joshi 's code, you had no problems.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Products

Release

R2022a

Tags

Asked:

on 22 Sep 2022

Edited:

on 22 Sep 2022

Community Treasure Hunt

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

Start Hunting!