The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+16 votes

The running time of the following algorithm

Procedure $A(n)$

If $n \leqslant 2$ return ($1$) else return $(A( \lceil  \sqrt{n}  \rceil))$;

is best described by

  1. $O(n)$
  2. $O(\log n)$
  3. $O(\log \log n)$
  4. $O(1)$
asked in Algorithms by Veteran (68.8k points)
edited by | 1.7k views
how to solve this question..?

2 Answers

+34 votes
Best answer
The complexity will be the number of times the recursion happens which is equal to the number of times we can take square root of n recursively, till n becomes $2$.

$T(n) = T\left(\lceil \sqrt n \rceil \right) + 1$

$T(2) = 1$
$T\left(2^2\right) =  T(2) + 1 = 2$
$T\left(2^{2^2}\right) =  T(4) + 1 = 3$
$T\left(2^{2^3}\right) =  T(16) + 1 = 4$

So, $T(n) = \lg\lg n + 1 = O\left(\log \log n\right)$
answered by Veteran (332k points)
edited by
why is it +1 and not some constant c in recurrence relation
why we are adding constant in recurrence relation?
because u will do some constant amount of work at every recurrence

Read this article once, then you may not need to solve this again ever, it becomes very intuitive.

+9 votes
log log n.
substitute root n = m then proceed.
answered by Veteran (49.2k points)

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,620 questions
39,267 answers
36,656 users