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 ifs
andn
fit together. E.g. if0 < n < s
holds the algorithm will not terminate. - How long does it take, if it terminates?
k
is initialized with0
and becomes the value ofs
after the first iteration. From there it is basically cubed every iteration. Solvings^(3^x) = n
leads to a complexity ofΘ(log log n)
.
solved Can anyone help me figure out what this algorithm does? [closed]