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:
- Does it terminate? Under what conditions?
Only ifsandnfit together. E.g. if0 < n < sholds the algorithm will not terminate. - How long does it take, if it terminates?
kis initialized with0and becomes the value ofsafter the first iteration. From there it is basically cubed every iteration. Solvings^(3^x) = nleads to a complexity ofΘ(log log n).
solved Can anyone help me figure out what this algorithm does? [closed]