这个短代码的大O

Big O of this short code

本文关键字:代码      更新时间:2023-10-11

我需要确定这个短代码的大O:

var iterations = 0;
function operation(num){
    iterations++;
    if (num == 0) return 1;
    return operation(Math.floor((num / 10) * 2));
}
var result = operation(1000);
alert('Result = ' + result + ', number of iterations = ' + iterations);

我想出了一些关于O(log(logN))的东西,但我不确定。你能帮我一下吗?

http://jsfiddle.net/qotbu5pq/2/

[注释中的回答]

  • 你几乎把运算除以5,直到得到零的结果
  • 所以不应该是~log5(N)迭代,这意味着O(log(N))
  • 对不起,我不想加这么琐碎的答案