i=12
while( i < n )
{
i=i^5
i=i/3
i=iā7
}
What is the complexity of the given program?
i=12
while( i < n )
{
i=i^5
i=i/3
i=iā7
}
What is the complexity of the given program?
Let $V(0)=12$ and $V(j)$ be the value of the variable $i$ immediately after the $j$-th iteration for $j \ge 1$.
$$ \begin{align} V(j) &= \frac{7}{3} \cdot V(j-1)^5 \\ &= \left(\frac{7}{3}\right)^{1+5} \cdot V(j-2)^{25} \\ &= \left(\frac{7}{3}\right)^{1+5+25} \cdot V(j-3)^{125} \\ &= \dots \\ & = \left(\frac{7}{3}\right)^{\sum_{k=1}^{j-1} 5^k} + V(0)^{5^j} \\ &= \left(\frac{7}{3}\right)^{\frac{5^j - 1}{4}} + 12^{5^j} \end{align} $$ (I'm hand-waving a bit when the dots are used. If you want a formal proof you can use induction on $j$).
For your algorithm to stop you need that $V(j) \ge n$. When does this happen?
Notice that $V(j)$ is monotonically increasing. Then, you need more than $s = \lfloor \log_{5} \log_{12} n/2 \rfloor$ iterations since: $$V(s) = \left(\frac{7}{3}\right)^{\frac{5^s}{4}} + 12^{5^s} < 2 \cdot 12^{5^s} \le n,$$ and at most $S = \lceil \log_{5} \log_{12} n \rceil$ iterations since: $$ V(S) = \left(\frac{7}{3}\right)^{\frac{5^S}{4}} + 12^{5^S} > 12^{5^S} \ge n. $$
Each iteration requires constant time (assuming that each arithmetic operation can be carried out in $O(1)$ time, which might or might not be reasonable if you're dealing with large values). Therefore the time complexity is $\Theta(\log \log n)$.