How can I express this algorithm through summation

51 Views Asked by At

I have the following algorithm and I want to find Big-Oh but how can I express this series in summation form:

func()
{
    i=1; s=1;
    while(s<=n)
    {
        i++;
        s = s + i;
        print("something");
    }
}

$\qquad\begin{array}{c|cccccccc} i & 1 & 2 & 3 & 4 & 5 & 6 & . . . & k \\ \hline s & 1 & 3 & 6 & 10 & 15 & 21 & ... & n \end{array} $

By following the series above I can see $i = \frac{k(k+1)}{2}$ and $s_{i} = n$:

$\frac{k(k+1)}{2} > n => O(\sqrt{n})$

How can I write this in summation form:

$$\sum_{s=1}^{n}of what$$

1

There are 1 best solutions below

3
On

You seem to be confusing the variable names in your question, but:

You are summing up values of $i$ from $1$ to $n$, so

$$S = \sum_{i = 1}^{k}i$$

which has the explicit formula $\frac{k(k+1)}{2}$ as you say.