Iterated Logarithm log*(n)

Iterated Logarithm or Log*(n) is the number of times the logarithm function must be iteratively applied before the result is less than or equal to 1.

   \log ^{*}n:={\begin{cases}0}n\leq 1;\\1+\log ^{*}(\log n)}n>1\end{cases}}}

Applications: It is used in analysis of algorithms (Refer Wiki for details)

C++

// Recursive CPP program to find value of
// Iterated Logarithm
#include <bits/stdc++.h>
using namespace std;
  
int _log(double x, double base)
{
    return (int)(log(x) / log(base));
}
  
double recursiveLogStar(double n, double b)
{
    if (n > 1.0)
        return 1.0 + recursiveLogStar(_log(n, b), b);
    else
        return 0;
}
  
// Driver code
int main()
{
    int n = 100, base = 5;
    cout << "Log*(" << n << ") = " 
         << recursiveLogStar(n, base) << "\n";
    return 0;
}

Java

// Recursive Java program to 
// find value of Iterated Logarithm
import java.io.*;
  
class GFG
{
static int _log(double x, 
                double base)
{
    return (int)(Math.log(x) / 
                 Math.log(base));
}
  
static double recursiveLogStar(double n, 
                               double b)
{
    if (n > 1.0)
        return 1.0
               recursiveLogStar(_log(n, 
                                 b), b);
    else
        return 0;
}
  
// Driver code
public static void main (String[] args) 
{
    int n = 100, base = 5;
    System.out.println("Log*(" + n + ") = " +
                  recursiveLogStar(n, base));
}
}
  
// This code is contributed by jit_t

PHP

<?php
// Recursive PhP program to find 
// value of Iterated Logarithm
  
function _log($x, $base)
{
    return (int)(log($x) / log($base));
}
  
function recursiveLogStar($n, $b)
{
    if ($n > 1.0)
        return 1.0 + 
               recursiveLogStar(_log($n
                               $b), $b);
    else
        return 0;
}
  
// Driver code
$n = 100; $base = 5;
echo "Log*(" , $n , ")"," = ",
recursiveLogStar($n, $base), "\n";
  
// This code is contributed by ajit
?>


Output :

Log*(100) = 2

Iterative Implementation :

// Iterative CPP function to find value of
// Iterated Logarithm
int iterativeLogStar(double n, double b)
{
    int count = 0;
    while (n >= 1) {
        n = _log(n, b);
        count++;
    }
    return count;
}

This article is contributed by Abhishek rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : jit_t




Practice Tags :
Article Tags :

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)






User Actions