Open In App

Find the difference between two numbers using log function

Last Updated : 25 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two integers a and b, the task is to find the subtraction of a and b i.e, (a-b) using the log function.

Note: We can not use the - operator.

Examples:

Input: a = 4, b = 81
Output: -77

Input: a = -3, b = 16  
Output:  -19

Input: a = -3, b = -13  
Output: 10

Approach: To write the solution to this question, use the following three properties of the log

  • log( a / b) = log( a ) - log( b )
  • log a( a )  = 1
  • log ( ab ) = b log ( a )

Combine these three properties and write.

loge ( ea / eb )
=> loge ( ea )  - loge ( eb )  {using property 1}
= >  a loge ( e )  - bloge ( e )  {using property 2}
= > a - b {using property 3}

Note: e is the Euler number i.e, 2.71828 

Follow the steps below to solve the problem:

  • The subtraction of a-b will be log(exp(a) / exp(b)) as the answer.

Below is the implementation of the above approach.

C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to find the subtraction
// of two number a and b i.e, (a-b)
int subtract(int a, int b)
{
    return log(exp(a) / exp(b));
}

// Driver Code
int main()
{
    int a = -15;
    int b = 7;

    cout << subtract(a, b);

    return 0;
}
Java
// Java program for the above approach
class GFG {

    // Function to find the subtraction
    // of two number a and b i.e, (a-b)
    static int subtract(int a, int b) {
        return (int) Math.log(Math.exp(a) / Math.exp(b));
    }

    // Driver Code
    public static void main(String[] args) {
        int a = -15;
        int b = 7;

        System.out.print(subtract(a, b));

    }
}

// This code is contributed by shikhasingrajput 
Python3
# Python3 program for the above approach
import math

# Function to find the subtraction
# of two number a and b i.e, (a-b)
def subtract(a, b) :
    return math.log(math.exp(a) / math.exp(b));

# Driver Code
if __name__ == "__main__" :

    a = -15;
    b = 7;

    print(subtract(a, b));
    
    # This code is contributed by AnkThon
C#
// C# program for the above approach
using System;

public class GFG {

    // Function to find the subtraction
    // of two number a and b i.e, (a-b)
    static int subtract(int a, int b)
    {
        return (int)Math.Log(Math.Exp(a) / Math.Exp(b));
    }

    // Driver Code
    public static void Main(string[] args) {
        int a = -15;
        int b = 7;

        Console.Write(subtract(a, b));
    }
}

// This code is contributed by AnkThon
JavaScript
<script>
        // JavaScript Program to implement
        // the above approach


        // Function to find the subtraction
        // of two number a and b i.e, (a-b)
        function subtract(a, b) {
            return Math.log(Math.exp(a) / Math.exp(b));
        }

        // Driver Code

        let a = -15;
        let b = 7;

        document.write(subtract(a, b));

    // This code is contributed by Potta Lokesh
    </script>

 
 


Output
-22


 

Time Complexity: O(1)
Auxiliary Space: O(1)


 


Next Article
Article Tags :
Practice Tags :

Similar Reads