Logarithm - GeeksforGeeks

05 Jan, 2022
Logarithmic function is the inverse to the exponential function. A logarithm to the base b is the power to which b must be raised to produce a given number. For example,  is equal to the power to which 2 must be raised to in order to produce 8. Clearly, 2^3 = 8 so  = 3. In general, for b > 0 and b not equal to 1.Fact about Logarithm :
Logarithms were quickly adopted by scientists because of various useful properties that simplified long, tedious calculations.Logarithm to base 10 (that is b = 10) is called the common logarithm and has many applications in science and engineering.Natural logarithm, is a logarithm with base e. It is used in mathematics and physics, because of its simpler derivative.Binary logarithm is a logarithm with base 2 and is commonly used in computer science.
Logarithms were quickly adopted by scientists because of various useful properties that simplified long, tedious calculations.
Logarithm to base 10 (that is b = 10) is called the common logarithm and has many applications in science and engineering.
Natural logarithm, is a logarithm with base e. It is used in mathematics and physics, because of its simpler derivative.
Binary logarithm is a logarithm with base 2 and is commonly used in computer science.
Laws of Logarithms :
How to find logarithm of number?
Naive solution:The idea is to create a function that calculates and returns . For example, if n = 64, then your function should return 6, and if n = 129, then your function should return 7.
C
Java
Python3
C#
PHP
Javascript
// C program to find log(n) using Recursion#include <stdio.h>  unsigned int Log2n(unsigned int n){    return (n > 1) ? 1 + Log2n(n / 2) : 0;}  int main(){    unsigned int n = 32;    printf("%u", Log2n(n));    getchar();    return 0;}
// Java program to find log(n)// using Recursionclass Gfg1 {      static int Log2n(int n)    {        return (n > 1) ? 1 + Log2n(n / 2) : 0;    }      // Driver Code    public static void main(String args[])    {        int n = 32;        System.out.println(Log2n(n));    }}
# Python 3 program to# find log(n) using Recursion  def Log2n(n):      return 1 + Log2n(n / 2) if (n > 1) else 0  # Driver coden = 32print(Log2n(n))
// C# program to find log(n)// using Recursionusing System;  class GFG {      static int Log2n(int n)    {        return (n > 1) ? 1 + Log2n(n / 2) : 0;    }      // Driver Code    public static void Main()    {        int n = 32;          Console.Write(Log2n(n));    }}
<?php// PHP program // to find log(n) using Recursion  function Log2n($n){    return ($n > 1) ? 1 + Log2n($n / 2) : 0;}  // Drive main      $n = 32;    echo Log2n($n);?>
<script>  // Javascript program to find log(n)// using Recursionfunction Log2n(n){    return (n > 1) ? 1 + Log2n(n / 2) : 0;}  // Driver Codevar n = 32;document.write(Log2n(n));  // This code is contributed by 29AjayKumar  </script>
5

Time complexity: O(log n)Auxiliary space: O(log n) if the stack size is considered during recursion otherwise O(1)
Efficient solutions:
Using inbuilt log Function
Question 1: Find the value of x in equation given 8x+1 – 8x-1 = 63Solution: Take 8x-1 common from the eq.It reduce to8x-1(82 – 1) = 638x-1 = 1Hence, x – 1 = 0x = 1
Question 2: Find the value of x for the eq. given log0.25x = 16Solution: log0.25x = 16It can be write asx = (0.25)16x = (1/4)16x = 4-16
Question 3: Solve the equation log121728 x log96561Solution: It can be written aslog12(123) x log9(94)= 3log1212 x 4log99= 3 x 4 = 12
Question 4: Solve for xlogx3 + logx9 + logx27 + logx81 = 10
Solution: It can be write aslogx(3 x 9 x 27 x 81) = 10logx(31 x 32 x 33 x 34) = 10logx(310) = 1010 logx3 = 10then, x = 3
Question 5: If log(a + 3 ) + log(a – 3) = 1 ,then a=?Solution: log10((a + 3)(a – 3))=1log10(a2 – 9) = 1(a2 – 9) = 10a2 = 19a = √19
Question 6: Solve 1/logab(abcd) + 1/logbc(abcd) + 1/logcd(abcd) + 1/logda(abcd)Solution:=logabcd(ab) + logabcd(bc) + logabcd(cd) + logabcd(da)=logabcd(ab * bc * cd * da)=logabcd(abcd)2=2 logabcd(abcd)=2
Question 7: If xyz = 10 , then solve log(xn yn / zn) + log(yn zn / xn) + log(zn xn / yn)Solution:log(xn yn / zn * yn zn / xn * zn xn / yn)= log xn yn zn= log(xyz)n= log10 10n= n
Question 8: Find (121/10)x = 3Solution: Apply logarithm on both sideslog(121/10)(121/10)x = log(121/10)3x = (log 3) / (log 121 – log 10)x = (log 3) / (2 log 11 – 1)
Question 9: Solve log(2x2 + 17)= log (x – 3)2Solution:log(2x2 + 17)= log (x2 – 6x + 9)2x2 + 17 = x2 – 6x + 9x2 + 6x + 8 = 0x2 + 4x + 2x + 8 = 0x(x + 4) + 2(x + 4) = 0(x + 4)(x + 2)=0x= -4,-2
Question 10: log2(33 – 3x)= 10log(5 – x). Solve for x.Solution: Put x = 0log2(33 – 1)= 10log(5)log232 = 55 log2 2 = 55 = 5LHS = RHS
 More problems related to Logarithm :
Find minimum number of Log value needed to calculate Log upto N
Find maximum among x^(y^2) or y^(x^2) where x and y are given
Print all substring of a number without any conversion
Program to compare m^n and n^m
Find larger of x^y and y^x
Recent Articles on Logarithm!
Mithun Kumar
singhpulkit
ujjwal17
29AjayKumar
maths-log
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Program to find GCD or HCF of two numbers
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Sieve of Eratosthenes
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
Constructors in C++