
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
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.
- Related Articles
- Python Program to calculate n+nm+nmm.......+n(m times).
- If \( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} \) and \( c=x^{l+m} y^{n} \), prove that \( a^{m-n} b^{n-1} c^{l-m}=1 . \)
- If \( x=a^{m+n}, y=a^{n+1} \) and \( z=a^{l+m} \), prove that \( x^{m} y^{n} z^{l}=x^{n} y^{l} z^{m} \)
- Construct PDA for L = {0n1m2(n+m) | m,n >=1}
- Calculate n + nn + nnn + β¦ + n(m times) in Python program
- If $l, m, n$ are three lines such that $l \parallel m$ and $n \perp l$, prove that $n \perp m$.
- Construct DPDA for anbmc(n+m) n,mβ₯1 in TOC
- Calculate n + nn + nnn + u + n(m times) in Python Program
- Construct DPDA for a(n+m)bmcn n,mβ₯1 in TOC
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- If $m$ and $n$ are the zeroes of the polynomial $3x^2+11xβ4$, find the values of $\frac{m}{n}+\frac{n}{m}$
- Construct Pushdown automata for L = {0n1m2(n+m) | m,n = 0} in C++
- It is given that\[63.63=m\left(21+\frac{n}{100}\right)\]Where \( m, n \) are positive integers and \( n
- Simplify:\( \sqrt[lm]{\frac{x^{l}}{x^{m}}} \times \sqrt[m n]{\frac{x^{m}}{x^{n}}} \times \sqrt[n l]{\frac{x^{n}}{x^{l}}} \)
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
