0

I am looking for the run time of the following pseudo code.

FunctionA(n)
a ← 2
b ← 2n²
while b>1
    a ← a +1 
    c ← n
    while c ≥ n
        b ← b - a
        a ← a/2
        c ← 2n/c

So I think the inner while loop has a run time complexity which will be definde by the outer loop since c is allways 2 and because it allways executes as long as the outer loop runs. The outer loop decreases b with every step by 1 and a. a is allways the half of the precursor. Therefor I have the feeling that the outer loop runs $\Theta(log_2(n))$ and hence the result is $\Theta(2*log_2(n))$. Would somebody comfirm this?

gamma
  • 125
  • 3
  • We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. – D.W. May 08 '19 at 16:33
  • If n = 1 the program runs forever. Your analysis seems way off. I'd say O (n^2). Not a clue where you got a log from. – gnasher729 May 08 '19 at 21:32
  • Analyse the inner loop for the cases n = 1, n = 2, and n ≥ 3. – gnasher729 May 08 '19 at 21:34
  • Yes, I noticed that it's an Infinite loop for n = 1 and n = 2 but why is it $\mathcal{O}(n^2)$? – gamma May 08 '19 at 22:11

0 Answers0