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.
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.
m^n < n^m
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program to compare two fractions
- C++ program to compare two Strings using Operator Overloading
- std::string::compare() in C++
- Compare sum of first N-1 elements to Nth element of an array
- Difference between Relational operator(==) and std::string::compare() in C++
- Compare numbers represented by Linked Lists
- Problem in comparing Floating point numbers and how to compare them correctly?
- Hello World Program : First program while learning Programming
- Program for n-th odd number
- Program for n-th even number
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Write a URL in a C++ program
- Output of C Program | Set 29
- C program to calculate the value of nPr
- Program to add two polynomials
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.