having trouble with for loops
Mostrar comentarios más antiguos
Having Trouble...
'Sum of N^2 with N = 1 to 1,000 using a for loop'
Respuesta aceptada
Más respuestas (2)
Roger Stafford
el 13 de Feb. de 2014
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
el 29 de Oct. de 2022
0 votos
a = 1;
n= 1000;
for i = 1: n
a = a+i^2
end
disp(a)
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!