I'm trying to write a simple program that computes the sum of the series 1^2 + 2^2 + 3^2... such that the sum is as large as possible without exceeding 1000. I need to use a while loop in this program. Here's my code so far:

clear;

number=0;

sum_vector=[];

End_flag=0

while End_flag==0;

number=number+1

square=number.^2

sum_vector(number)=square

sum=sum(sum_vector)

if sum>=1000

End_flag=1;

end

end

So my basic idea here was to place each number squared into an array, then sum the array and check to see if it makes the sum condition (<= 1000) and if not go up and plug the next number into the array etc. But this doesn't work and I'm not sure why. Any pointers?

Thanks,

Kim