フィルターのクリア

While loop for sums?

8 ビュー (過去 30 日間)
Miranda Moore
Miranda Moore 2016 年 12 月 1 日
コメント済み: Walter Roberson 2022 年 10 月 23 日
Add up the squares of all odd positive integers until it equals or exceeds 5 million.
(1^2+3^2...)
  2 件のコメント
Steven Lord
Steven Lord 2016 年 12 月 1 日
Show what you've tried to do to solve the problem and ask a specific question about where you're having difficulty and you may receive some guidance.
Rena Berman
Rena Berman 2017 年 1 月 20 日
(Answers Dev) Restored Question.

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2016 年 12 月 1 日
Try this:
theSum = 0; % Initialize
thisNumber = -1;
while theSum < 5000000
thisNumber = thisNumber + .......
theSum = theSum + ......
end
I've given you a start. Please finish the rest of your homework yourself.

s.p4m
s.p4m 2016 年 12 月 1 日
編集済み: s.p4m 2016 年 12 月 2 日
sum=0;
k=0;
while(sum<=5*10^6)
if(mod(k,2))
sum=sum+k^2;
end
k=k+1;
end
Next time please try to solve your homework by yourself
  5 件のコメント
s.p4m
s.p4m 2016 年 12 月 2 日
Thanks for the answer. You are right with every point.
I didn't know about the rule not to do outher people homework, but I will embrace it from now on.
Jan
Jan 2016 年 12 月 2 日
編集済み: Jan 2016 年 12 月 2 日
5. 5*10^6 is an expensive power operation, while 5e6 is cost free constant.
Thanks, s.p4m, for you suggestion. If the OP reads the comments carefully, he has learned something about programming. :-)

サインインしてコメントする。


prin
prin 2022 年 10 月 23 日
jum=100; n = 1; while sum(1:r) > jum
disp(r)
n = n - 1;
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 10 月 23 日
r is not defined. You are missing the "end" of the "while".

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by