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
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Program to compare two fractions
- C++ program to compare two Strings using Operator Overloading
- C program to Compare Two Strings without using strcmp() function
- Difference between Relational operator(==) and std::string::compare() in C++
- Problem in comparing Floating point numbers and how to compare them correctly?
- std::string::compare() in C++
- Compare sum of first N-1 elements to Nth element of an array
- Compare numbers represented by Linked Lists
- Efficient ways to compare a variable with multiple values
- Hello World Program : First program while learning Programming
- Program to find if two numbers and their AM and HM are present in an array using STL
- Write a program that produces different results in C and C++
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Program to find Normal and Trace of a matrix
- C program to demonstrate fork() and pipe()
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Program for quotient and remainder of big number
- Discrete Cosine Transform (Algorithm and Program)
- C++ Program to print current Day, Date and Time
- How to compile 32-bit program on 64-bit gcc in C and C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.