Program to compute log a to any base b (logb a)

Given two integers a and b, the task is to find the log of a to any base b, i.e. logb a.

Examples:

Input: a = 3, b = 2
Output: 1

Input: a = 256, b = 4
Output: 4

Using inbuilt log function

  1. Find the log of a to the base 2 with the help of log() method
  2. Find the log of b to the base 2 with the help of log() method
  3. Divide the computed log a from the log b to get the logb a, i.e, \LARGE log_{b}\text{ } a = \frac{log\text{ }a}{log\text{ }b}

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find log(a) on any base b
#include <bits/stdc++.h>
using namespace std;
  
int log_a_to_base_b(int a, int b)
{
    return log(a) / log(b);
}
  
// Driver code
int main()
{
    int a = 3;
    int b = 2;
    cout << log_a_to_base_b(a, b) << endl;
  
    a = 256;
    b = 4;
    cout << log_a_to_base_b(a, b) << endl;
  
    return 0;
}
  
// This code is contributed by shubhamsingh10

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find log(a) on any base b
  
#include <math.h>
#include <stdio.h>
  
int log_a_to_base_b(int a, int b)
{
    return log(a) / log(b);
}
  
// Driver code
int main()
{
    int a = 3;
    int b = 2;
    printf("%d\n",
           log_a_to_base_b(a, b));
  
    a = 256;
    b = 4;
    printf("%d\n",
           log_a_to_base_b(a, b));
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find log(a) on any base b 
class GFG 
{
      
    static int log_a_to_base_b(int a, int b) 
    
        return (int)(Math.log(a) / Math.log(b)); 
    
      
    // Driver code 
    public static void main (String[] args)
    
        int a = 3
        int b = 2
        System.out.println(log_a_to_base_b(a, b)); 
      
        a = 256
        b = 4
        System.out.println(log_a_to_base_b(a, b)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find log(a) on any base b
from math import log
  
def log_a_to_base_b(a, b) :
    return log(a) // log(b);
  
# Driver code
if __name__ == "__main__" :
      
    a = 3;
    b = 2;
    print(log_a_to_base_b(a, b));
  
    a = 256;
    b = 4;
    print(log_a_to_base_b(a, b));
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find log(a) on any base b 
  
using System;
  
public class GFG 
{
      
    static int log_a_to_base_b(int a, int b) 
    
        return (int)(Math.Log(a) / Math.Log(b)); 
    
      
    // Driver code 
    public static void Main()
    
        int a = 3; 
        int b = 2; 
        Console.WriteLine(log_a_to_base_b(a, b)); 
      
        a = 256; 
        b = 4; 
        Console.WriteLine(log_a_to_base_b(a, b)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

1
4

Using Recursion

  1. Recursively divide a by b till a is greater than b.
  2. Count the number of times the divide is possible. This is the log of a to the base b, i.e. logb a

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find log(a) on
// any base b using Recursion
#include <iostream>
using namespace std;
  
// Recursive function to compute
// log a to the base b
int log_a_to_base_b(int a, int b)
{
    return (a > b - 1)
            ? 1 + log_a_to_base_b(a / b, b)
            : 0;
}
  
// Driver code
int main()
{
    int a = 3;
    int b = 2;
    cout << log_a_to_base_b(a, b) << endl;
  
    a = 256;
    b = 4;
    cout << log_a_to_base_b(a, b) << endl;
  
    return 0;
}
  
// This code is contributed by shubhamsingh10

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find log(a) on
// any base b using Recursion
  
#include <stdio.h>
  
// Recursive function to compute
// log a to the base b
int log_a_to_base_b(int a, int b)
{
    return (a > b - 1)
               ? 1 + log_a_to_base_b(a / b, b)
               : 0;
}
  
// Driver code
int main()
{
    int a = 3;
    int b = 2;
    printf("%d\n",
           log_a_to_base_b(a, b));
  
    a = 256;
    b = 4;
    printf("%d\n",
           log_a_to_base_b(a, b));
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find log(a) on
// any base b using Recursion
class GFG
{
      
    // Recursive function to compute
    // log a to the base b
    static int log_a_to_base_b(int a, int b)
    {
        int rslt = (a > b - 1)? 1 + log_a_to_base_b(a / b, b): 0;
        return rslt;
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int a = 3;
        int b = 2;
        System.out.println(log_a_to_base_b(a, b));
      
        a = 256;
        b = 4;
        System.out.println(log_a_to_base_b(a, b));
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find log(a) on 
# any base b using Recursion 
  
# Recursive function to compute 
# log a to the base b 
def log_a_to_base_b(a, b) : 
  
    rslt = (1 + log_a_to_base_b(a // b, b)) if (a > (b - 1)) else 0
              
    return rslt;
      
# Driver code 
if __name__ == "__main__"
  
    a = 3
    b = 2
    print(log_a_to_base_b(a, b)); 
  
    a = 256
    b = 4
    print(log_a_to_base_b(a, b)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find log(a) on
// any base b using Recursion
using System;
  
class GFG
{
      
    // Recursive function to compute
    // log a to the base b
    static int log_a_to_base_b(int a, int b)
    {
        int rslt = (a > b - 1)? 1 + log_a_to_base_b(a / b, b): 0;
        return rslt;
    }
      
    // Driver code
    public static void Main() 
    {
        int a = 3;
        int b = 2;
        Console.WriteLine(log_a_to_base_b(a, b));
      
        a = 256;
        b = 4;
        Console.WriteLine(log_a_to_base_b(a, b));
    }
}
  
// This code is contributed by Yash_R

chevron_right


Output:

1
4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.