Program to compare m^n and n^m

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=1000

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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^m 
void 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 Code 
int main() {
      
    unsigned long long m = 987654321, n = 123456987;
      
    // function call to compare m^n and n^m
    check(m, n);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to compare which 
// is greater m^n or n^m 
import java .io.*;
  
class GFG
{
// function to compare
// m^n and n^m 
static 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 Code 
static 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to compare 
# which is greater m^n or n^m 
import math
  
# function to compare
# m^n and n^m 
def 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 Code 
m = 987654321
n = 123456987;
  
# function call to 
# compare m^n and n^m
check(m, n);
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to compare which  
// is greater m^n or n^m 
using System;
  
class GFG
{
// function to compare
// m^n and n^m 
static 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 Code 
static 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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 
    $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^m
check($m, $n);
  
// This code is contributed by anuj_67.
?>

chevron_right



Output :

m^n < n^m


My Personal Notes arrow_drop_up

Dream it Do it

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.