# I need help finding the complexity of an algorithm

i have the following algorithm :

j = 1 while j ≤ n do j = 2^j

where n is our input and is a non negative integer

I found the complexity of the algorithm to be O(1) . Can someone tell me if this is correct and if not can you tell me the right answer and explain the way you found it? Thank you.

• Without a problem solved by above procedure, there is no algorithm. Anyway, just execute it mentally, using pen&paper or any means giving you some visual impression, for values of, say, 42, 2022 and 8000000000 (approx. #live humans): does the effort change? Apr 7, 2022 at 8:05
• oeis.org/A014221. I think this might help. Apr 7, 2022 at 12:06
• 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.
Apr 17, 2022 at 1:42

The base-2 iterated logarithm function $$\log^*(n)$$ is the number of times you need to apply the logarithm function to obtain a value that is at most $$1$$. It is a very slowly growing function. The answer seems to be $$O(\log^* n)$$.

See [CLRS, Section 3.2, p. 58-59], which states that the number of atoms in the observable universe is about $$10^{80}$$, which is much smaller than $$2^{65536}$$ (whose iterated logarithm is $$5$$). Hence, for any practical input size $$n$$, your algorithm would have at most $$5$$ iterations, and so it is ok to take the running time of the algorithm to be $$O(1)$$.

• Technically $O(1)$ still implies a behavior for $n$ to infinity. It is better to say that the running time is bounded in practice.
– user16034
Sep 18, 2022 at 16:16

No, the complexity cannot be constant, because the iterations $$j = 2^j$$ always yield a finite integer, and there is always a larger $$n$$. So the number of iterations is unbounded.

Anyway, as the required n grow astronomically fast (see below), the number of iterations grows extremely slowly.

The iterates are

$$1, 2, 4, 16, 65536, 2.0035299304\cdots\cdot 10^{19728},$$ and there is no way to write down the next value.