[Solved] Can anyone help me figure out what this algorithm does? [closed]


At the end of the for loop s and k are equal. Before the next iteration k != n is checked. This is equivalent to s != n. So the loop runs until s == n holds and then n is returned. So the function get the input n, runs for some time and returns n at the end.

The questions are:

  1. Does it terminate? Under what conditions?
    Only if s and n fit together. E.g. if 0 < n < s holds the algorithm will not terminate.
  2. How long does it take, if it terminates?
    k is initialized with 0 and becomes the value of s after the first iteration. From there it is basically cubed every iteration. Solving s^(3^x) = n leads to a complexity of Θ(log log n).

solved Can anyone help me figure out what this algorithm does? [closed]