- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to compare m^n and n^m
The problem statement states that we need to write a program to compare m^n and n^m. We need to figure out the algorithm to calculate $m^{n}$ and $n^{m}$ and compare them and print accordingly if $m^{n}$ is greater than $n^{m}$, or if $m^{n}$ is less than $n^{m}$ , or if they both are equal. We will be given two positive numbers, m and n and we need to find out $m^{n}$ and $n^{m}$ and compare both the values.
For example,
INPUT : m=2 , n=5
OUTPUT : m^n is greater than n^m.
Explanation : $m^{n}$ which is 25 = 32 and $n^{m}$ which is 52 = 25. Since 32 is greater than 25, we will print m^n is greater than n^m.
INPUT : m=6 , n=4
OUTPUT : n^m is greater than m^n.
Explanation : $m^{n}$ is 64 which is equal to 1296 and $n^{m}$ is 46 which is equal to 4096. Since 4096 is greater than 1296, we will print n^m is greater than m^n.
Algorithm
The basic approach that comes to mind after seeing the problem or the naive approach to the problem is calculating the values of $m^{n}$ πππ $n^{m}$. After calculating the values for both then compare them using the if statement and print accordingly. We can calculate the values of $m^{n}$ πππ $n^{m}$ using the pow function in C++.
Syntax
int value= pow(m,n)
The first value passed in the function represents the base and the second value passed represents the power. But this approach can only be used for the smaller values of m and n since calculating values of $m^{n}$ πππ $n^{m}$ will cause overflow in int data type for larger values of m and n.
So, the efficient approach to the problem can be using a log function. Letβs explain the approach with an example. For comparing the values of $m^{n}$ πππ $n^{m}$, We can use logs on both sides. It can be expressed as β
$$\log_{e}{(m^{n})}\:and\:\log_{e}{(n^{m})}$$
According to the laws of logarithmic function, this can be further written as
$$n\:\times\:\log_{e}{(m)}\:and\:m\:\times\:\log_{e}{(n)}$$
Now calculating these values and then comparing both the values will give us our required output. In C++, there is a built-in function called log which returns the natural log of any value passed through it. The natural log of any value is the logarithmic value with base e of the number.
Syntax
double ans= log(a)
It returns the natural log of a.
Below is the approach to solve the problem using this algorithm.
Approach
The steps that we are going to follow in this approach are β
Initialize a function where we will calculate and compare both the values.
Declare two variables.
Store the calculated values in the declared values.
Compare the values. If $m^{n}$ is greater than $n^{m}$, then print m^n is greater than n^m. Else if $n^{m}$ is greater than $m^{n}$ then print n^m is greater than m^n or if they are equal then print m^n is equal to n^m.
Example
The implementation of the approach in C++ β
#include <iostream> #include <bits/stdc++.h> using namespace std; //function to calculate and compare both the values void compare(int m, int n){ double m_n,n_m; //variables to store the calculated value m_n= n * (double)log(m); //type casting value of log into double to store decimal values since m is int data type n_m= m * (double)log(n); //comparing values using conditional statements and printing accordingly if(m_n>n_m){ cout<<"m^n is greater than n^m"<<endl; } else if(m_n<n_m){ cout<<"n^m is greater than m^n"<<endl; } else cout<<"m^n is equal to n^m"<<endl; } int main(){ int m=543,n=248; compare(m,n); m=10,n=40; compare(m,n); return 0; }
Output
n^m is greater than m^n m^n is greater than n^m
Time Complexity : O(1), constant time is taken.
Space Complexity : O(1), since no extra space is taken.
Conclusion
We discussed the approach to solve the problem to compare m^n and n^m. We use the log function in C++ to compare the values of m^n and n^m and print the answer accordingly.
I hope you find this article helpful and have cleared all your doubts regarding the problem and approach to solve the problem.