having trouble with for loops
显示 更早的评论
Having Trouble...
'Sum of N^2 with N = 1 to 1,000 using a for loop'
采纳的回答
更多回答(2 个)
Roger Stafford
2014-2-13
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
2022-10-29
0 个投票
a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)
类别
在 帮助中心 和 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!