What does the algorithm s = s + k * k do?

58 Views Asked by At

I just finished an exam in my math class and I did well except for one question that I just can't get out of my head, it seems simple but I just can't figure it out:

PROBLEM:

    s = 1, k = 1, n=4

    while k < n {

    DO:
    a) s = s + k * k
    b) k = k + 1

    } endWhile

    print s

QUESTION: what does this algorithm in general do for values of n?

I think it might have something to do with sum of squares.

Thanks in advance!

3

There are 3 best solutions below

0
On

Hint: It is adding square numbers to 1 $$s=1 + 1^2 + 2^2 + 3^2$$

0
On

$s$ begins with value 1, $k$ begins with value 1 too. while $k<n$, $k$ gets squared and adds to $s$. $k$ keep getting incremented and it stops when $k=n$ ($n^2$ is not added).

$$1+\sum_{k=1}^{n-1}k^2$$

0
On

For starting values $s_{0},k_{0}$: $$s(n) = s_{0}+\sum_{k=k_{0}}^{(n-1)} k^2 $$