Programa para calcular log a en cualquier base b (logb a)

Dados dos enteros ayb , la tarea es encontrar el logaritmo de a en cualquier base b, es decir , log b a .
Ejemplos: 
 

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

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

Usando la función log2 incorporada

  1. Encuentre el registro de a en base 2 con la ayuda del método log2() 
     
  2. Encuentra el logaritmo de b en base 2 con la ayuda del método log2() 
     
  3. Divida el logaritmo a calculado del logaritmo b para obtener el logaritmo b a , es decir,\LARGE log_{b}\text{ } a = \frac{log\text{ }a}{log\text{ }b}
     

A continuación se muestra la implementación del enfoque anterior. 
 

C++

// 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 log2(a) / log2(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, yousefonweb

C

// 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 log2(a) / log2(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;
}

Java

// 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

Python3

# Python3 program to find log(a) on any base b
from math import log2
 
def log_a_to_base_b(a, b) :
    return log2(a) // log2(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

C#

// 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

Javascript

<script>
 
// Javascript program to find log(a) on any base b
 
function log_a_to_base_b(a, b)
{
    return parseInt(Math.log(a) / Math.log(b));
}
 
// Driver code
var a = 3;
var b = 2;
document.write(log_a_to_base_b(a, b) + "<br>");
a = 256;
b = 4;
document.write(log_a_to_base_b(a, b));
 
// This code is contributed by rutvik_56.
</script>
Producción: 

1
4

 

Complejidad temporal: O(log b a)

Espacio Auxiliar: O(1)

Usando recursividad

  1. Divida recursivamente a por b hasta que a sea mayor que b. 
     
  2. Cuente el número de veces que la división es posible. Este es el logaritmo de a en base b, es decir, log b a 
     

A continuación se muestra la implementación del enfoque anterior. 
 

C++

// 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

C

// 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;
}

Java

// 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

Python3

# 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

C#

// 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

Javascript

<script>
// javascript program to find log(a) on
// any base b using Recursion   
 
// Recursive function to compute
    // log a to the base b
    function log_a_to_base_b(a , b)
    {
        var rslt = (a > b - 1) ? 1 + log_a_to_base_b(parseInt(a / b), b) : 0;
        return rslt;
    }
 
    // Driver code
        var a = 3;
        var b = 2;
        document.write(log_a_to_base_b(a, b)+"<br/>");
 
        a = 256;
        b = 4;
        document.write(log_a_to_base_b(a, b));
 
// This code is contributed by umadevi9616
</script>
Producción: 

1
4

 

Publicación traducida automáticamente

Artículo escrito por Code_r y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *