Program to compare m^n and n^m - GeeksforGeeks

12 May, 2021
Given two positive integers m and n, the task is to write a program that checks whether m^n is greater than, less than or equal to n^m.Examples :  
Input: m = 3, n = 10 Output: m^n > n^m Explanation : 3^10=59049 which is greater than 10^3=1000Input: m = 987654321, n = 123456987 Output: m^n < n^m 
A naive approach is to compute m^n and n^m, which causes overflow when m and n are very large. An efficient approach is to solve this problem using log.  
Given LHS = m^n and RHS = n^m. After taking log on both sides, LHS = n*log(m) and RHS = m*log(n) Then compare the LHS and RHS.
 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to compare which is greater// m^n or n^m #include <bits/stdc++.h>using namespace std; // function to compare m^n and n^mvoid check(unsigned long long m, unsigned long long int n)    {        // m^n        double RHS = m * (double)log(n);                 // n^m        double LHS = n * (double)log(m);                 if ( LHS > RHS )            cout << "m^n > n^m";                     else if ( LHS < RHS )            cout << "m^n < n^m";                     else            cout << "m^n = n^m";    } // Drivers Codeint main() {         unsigned long long m = 987654321, n = 123456987;         // function call to compare m^n and n^m    check(m, n);         return 0;}
// Java program to compare which// is greater m^n or n^mimport java .io.*; class GFG{// function to compare// m^n and n^mstatic void check(long m, long n){    // m^n    double RHS = m * (double)Math.log(n);         // n^m    double LHS = n * (double)Math.log(m);         if (LHS > RHS)        System.out.print("m^n > n^m");             else if (LHS < RHS)    System.out.print("m^n < n^m");             else        System.out.print("m^n = n^m");} // Driver Codestatic public void main (String[] args){    long m = 987654321, n = 123456987;     // function call to    // compare m^n and n^m    check(m, n);}} // This code is contributed by anuj_67.
# Python3 program to compare# which is greater m^n or n^mimport math # function to compare# m^n and n^mdef check( m, n):         # m^n    RHS = m * math.log(n);         # n^m    LHS = n * math.log(m);         if (LHS > RHS):        print("m^n > n^m");             elif (LHS < RHS):        print("m^n < n^m");             else:        print("m^n = n^m"); # Driver Codem = 987654321;n = 123456987; # function call to# compare m^n and n^mcheck(m, n); # This code is contributed by mits
// C# program to compare which // is greater m^n or n^musing System; class GFG{// function to compare// m^n and n^mstatic void check(ulong m, ulong n){    // m^n    double RHS = m * (double)Math.Log(n);         // n^m    double LHS = n * (double)Math.Log(m);         if (LHS > RHS)        Console.Write("m^n > n^m");             else if (LHS < RHS)    Console.Write("m^n < n^m");             else        Console.Write("m^n = n^m");} // Driver Codestatic public void Main (){    ulong m = 987654321, n = 123456987;     // function call to    // compare m^n and n^m    check(m, n); }} // This code is contributed by anuj_67.
<?php// PHP program to compare // which is greater m^n or n^m // function to compare// m^n and n^mfunction check( $m, $n){    // m^n    $RHS = $m * log($n);         // n^m    $LHS = $n * log($m);         if ( $LHS > $RHS )        echo "m^n > n^m";             else if ( $LHS < $RHS )    echo "m^n < n^m";             else        echo "m^n = n^m";} // Driver Code$m = 987654321;$n = 123456987; // function call to// compare m^n and n^mcheck($m, $n); // This code is contributed by anuj_67.?>
<script> // javascript program to compare which// is greater m^n or n^m // function to compare// m^n and n^m function check( m,  n){    // m^n    var RHS = m * Math.log(n);           // n^m    var LHS = n * Math.log(m);           if (LHS > RHS){        document.write("m^n > n^m");    }    else if (LHS < RHS) {    document.write("m^n < n^m");    }               else {        document.write("m^n = n^m");    }}   // Driver Code      var m = 987654321 ;      var n = 123456987;       // function call to    // compare m^n and n^m    check(m, n);     </script>
Output :  
m^n < n^m
 
vt_m
Mithun Kumar
bunnyram19
maths-log
C++
Mathematical
Mathematical
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Map in C++ Standard Template Library (STL)
Inheritance in C++
C++ Classes and Objects
Constructors in C++
Bitwise Operators in C/C++
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7