给定两个整数a和b ,任务是找到a到任何底数b的对数,即log b a 。
例子:
Input: a = 3, b = 2
Output: 1
Input: a = 256, b = 4
Output: 4
使用内置日志函数
- 借助log()方法找到以2为底的a的日志
- 借助log()方法找到b的对数以2为底的对数
- 将计算出的日志a从日志b中除以得到日志b a ,即
下面是上述方法的实现
C++
// C++ program to find log(a) on any base b
#include
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
C
// C program to find log(a) on any base b
#include
#include
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;
}
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 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
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
C++
// C++ program to find log(a) on
// any base b using Recursion
#include
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
// 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
输出:
1
4
使用递归
- 递归地将a除以b,直到a大于b。
- 计算可能的除法次数。这是a到基数b的对数,即log b a
下面是上述方法的实现
C++
// C++ program to find log(a) on
// any base b using Recursion
#include
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
// 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
输出:
1
4