Having Trouble...
'Sum of N^2 with N = 1 to 1,000 using a for loop'

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 13 日

0 投票

out = 1;
for ii = 2:1000
out = out + ii^2;
end

3 件のコメント

Joseph
Joseph 2014 年 2 月 13 日
SumN=0; % initializes N^2
for N=1:1000
SumN = SumN + N^2;
end
Still not giving me correct answer...Don't know what I'm doing wrong
Thomas
Thomas 2014 年 2 月 13 日
what answer do u expect?
Joseph
Joseph 2014 年 2 月 13 日
i got it thanks

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

その他の回答 (2 件)

Roger Stafford
Roger Stafford 2014 年 2 月 13 日

0 投票

Here's a way to check your answer:
n = 1000;
s = n*(n+1)*(2*n+1)/6;
This gives you the same value as
s = sum((1:n).^2);
Alonso Mukendi Kalonji
Alonso Mukendi Kalonji 2022 年 10 月 29 日

0 投票

a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by