-1

I know for a fact that algorithm A runs in $\Theta(\sqrt{n})$, but how does one derive that fact?

Algorithm A

i = 0
s = 0
while s <= n:
    s += i
    i += 1

Here is what I am thinking. We know that A is upper-bounded by O(n), as we increment $s$ by more than 1 in each iteration. We also know that it must be lower-bounded by $\log n$, as we increment $s$ with by something less than $2^i$ in each iteration. (Correct me if I am wrong, these are just my thoughts ...).

Now, what else can we say about A? How do we derive that its time complexity is $\Theta(\sqrt{n})$?

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43
  • Is that LaTeX syntax you're using for math? – Charles Duffy Sep 29 '19 at 17:32
  • @CharlesDuffy Yes – Sebastian Nielsen Sep 29 '19 at 17:32
  • 1
    Note that questions about theoretical computer science have their own Stack Exchange site, at [cs.se]; SO is narrowly focused on *writing code*, and tools exclusively for that use. – Charles Duffy Sep 29 '19 at 17:32
  • @CharlesDuffy I find it a little perplexing how other questions regarding time complexity is warmly welcomed on SO, when mine is not. (eg. https://stackoverflow.com/q/2095395/7123519 (a lot more theoretical than my question)) – Sebastian Nielsen Sep 29 '19 at 19:10
  • Our rules change over time -- and of course, some historical questions were added before [cs.se] existed at all. If you want an idea of current consensus, ask a question over on [meta], and feel free to link me to it; should there be a strong, current consensus there that questions about time complexity analysis are on-topic on SO proper, I'll honor it (as will others, when same is brought to attention). – Charles Duffy Sep 30 '19 at 13:44

3 Answers3

2

To help you in your reasoning you could do experimental tests to count the number of iterations. For example:

for n in range(100000000, 1000000000, 100000000) :   
    i = 0
    s = 0
    while s <= n:
        s += i
        i += 1

and get the following results:

    n       | iterations|   sqrt(n)
-------------------------------------
100000000   |   14143   |   10000.00
200000000   |   20001   |   14142.14
300000000   |   24496   |   17320.51
400000000   |   28285   |   20000.00
500000000   |   31624   |   22360.68
600000000   |   34642   |   24494.90
700000000   |   37418   |   26457.51
800000000   |   40001   |   28284.27
900000000   |   42427   |   30000.00

EDIT

As recommended by @WillNess You can learn more about it here

Massifox
  • 4,369
  • 11
  • 31
1

As you can see from the code s = 1 + 2 + 3 + .... + i (1) and s <= n (2). The first equation can also be written as s = i * (i + 1) / 2 which means that in i is approximately sqrt(s * 2) and sqrt(n * 2), and as we see from the code the while loop runs i time, each does a O(1) calculation. Therefore the overall complexity is O(sqrt(n))

ExplodingGayFish
  • 2,807
  • 1
  • 5
  • 14
1

Each time we increment i, we add s to i. This thus means that after k steps, s has grown to:

     k
    ---
    \
s = /   i
    ---
    i=0

This sequence is known. After the k-th step, there are Tk numbers, with Tk a triangular number [wiki]. A shorter formula for Tk is Tk=i×(i+1)/2. Tk thus scales quadratic with k.

The process will thus stop when Tk is higher than n. We can thus determine that: Tk > n, and thus k×(k+1)/2 > Tk and thus k2/2 + k/2 - n > 0. This is a quadratic equation with as discriminant d = 1/4 + 2×n, and thus as (positive) solution k0=-1/2 + √d. It thus scales with √2n.

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555