What is O(log* N)?

前端 未结 3 1527
南旧
南旧 2020-12-07 11:53

What is O(log* N) and how is it different from O(log N)?

3条回答
  •  不思量自难忘°
    2020-12-07 12:49

    The log* N bit is an iterated algorithm which grows very slowly, much slower than just log N. You basically just keep iteratively 'logging' the answer until it gets below one (E.g: log(log(log(...log(N)))), and the number of times you had to log() is the answer.

    Anyway, this is a five-year old question on Stackoverflow, but no code?(!) Let's fix that - here's implementations for both the recursive and iterative functions (they both give the same result):

    public double iteratedLogRecursive(double n, double b)
    {
        if (n > 1.0) {
            return 1.0 + iteratedLogRecursive( Math.Log(n, b),b );
        }
        else return 0;
    }
    
    public int iteratedLogIterative(double n, double b)
    {
        int count=0;
        while (n >= 1) {
            n = Math.Log(n,b);
            count++;
        }
        return count;
    }
    

提交回复
热议问题