Given two integer **X** and **Y**, the task is compare **X ^{Y}** and

**Y**for large values of

^{X}**X**and

**Y**.

**Examples:**

Input:X = 2, Y = 3

Output:2^3 < 3^2

2^{3}< 3^{2}

Input:X = 4, Y = 5

Output:4^5 > 5^4

**Naive approach:** A basic approach is to find the values **X ^{Y}** and

**Y**and compare them which can overflow as the values of X and Y can be large

^{X}**Better approach:** Taking log of both the equations, **log(X ^{Y}) = Y * log(X)** and

**log(Y**. Now, these values can be compared easily without overflows.

^{X}) = X * log(Y)Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to compare x^y and y^x ` `void` `compareVal(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Storing values OF x^y AND y^x ` ` ` `long` `double` `a = y * ` `log` `(x); ` ` ` `long` `double` `b = x * ` `log` `(y); ` ` ` ` ` `// Comparing values ` ` ` `if` `(a > b) ` ` ` `cout << x << ` `"^"` `<< y << ` `" > "` ` ` `<< y << ` `"^"` `<< x; ` ` ` ` ` `else` `if` `(a < b) ` ` ` `cout << x << ` `"^"` `<< y << ` `" < "` ` ` `<< y << ` `"^"` `<< x; ` ` ` ` ` `else` `if` `(a == b) ` ` ` `cout << x << ` `"^"` `<< y << ` `" = "` ` ` `<< y << ` `"^"` `<< x; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `double` `x = 4, y = 5; ` ` ` ` ` `compareVal(x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to compare x^y and y^x ` `static` `void` `compareVal(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Storing values OF x^y AND y^x ` ` ` `double` `a = y * Math.log(x); ` ` ` `double` `b = x * Math.log(y); ` ` ` ` ` `// Comparing values ` ` ` `if` `(a > b) ` ` ` `System.out.print(x + ` `"^"` `+ y + ` `" > "` `+ ` ` ` `y + ` `"^"` `+ x); ` ` ` ` ` `else` `if` `(a < b) ` ` ` `System.out.print(x + ` `"^"` `+ y + ` `" < "` `+ ` ` ` `y + ` `"^"` `+ x); ` ` ` ` ` `else` `if` `(a == b) ` ` ` `System.out.print(x + ` `"^"` `+ y + ` `" = "` `+ ` ` ` `y + ` `"^"` `+ x ); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `x = ` `4` `, y = ` `5` `; ` ` ` ` ` `compareVal(x, y); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `from` `math ` `import` `log ` ` ` `# Function to compare x^y and y^x ` `def` `compareVal(x, y) : ` ` ` ` ` `# Storing values OF x^y AND y^x ` ` ` `a ` `=` `y ` `*` `log(x); ` ` ` `b ` `=` `x ` `*` `log(y); ` ` ` ` ` `# Comparing values ` ` ` `if` `(a > b) : ` ` ` `print` `(x, ` `"^"` `, y, ` `">"` `, y, ` `"^"` `, x); ` ` ` ` ` `elif` `(a < b) : ` ` ` `print` `(x, ` `"^"` `, y, ` `"<"` `, y ,` `"^"` `, x); ` ` ` ` ` `elif` `(a ` `=` `=` `b) : ` ` ` `print` `(x, ` `"^"` `, y, ` `"="` `, y, ` `"^"` `, x); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `x ` `=` `4` `; y ` `=` `5` `; ` ` ` ` ` `compareVal(x, y); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to compare x^y and y^x ` `static` `void` `compareVal(` `double` `x, ` `double` `y) ` `{ ` ` ` ` ` `// Storing values OF x^y AND y^x ` ` ` `double` `a = y * Math.Log(x); ` ` ` `double` `b = x * Math.Log(y); ` ` ` ` ` `// Comparing values ` ` ` `if` `(a > b) ` ` ` `Console.Write (x + ` `"^"` `+ y + ` `" > "` `+ ` ` ` `y + ` `"^"` `+ x); ` ` ` ` ` `else` `if` `(a < b) ` ` ` `Console.Write (x + ` `"^"` `+ y + ` `" < "` `+ ` ` ` `y + ` `"^"` `+ x); ` ` ` ` ` `else` `if` `(a == b) ` ` ` `Console.Write (x + ` `"^"` `+ y + ` `" = "` `+ ` ` ` `y + ` `"^"` `+ x ); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `double` `x = 4, y = 5; ` ` ` ` ` `compareVal(x, y); ` `} ` `} ` ` ` `// This Code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

4^5 > 5^4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Recursive sum of digit in n^x, where n and x are very large
- Series summation if T(n) is given and n is very large
- GCD of two numbers when one of them can be very large
- Find (a^b)%m where 'a' is very large
- Check whether a very large number of the given form is a multiple of 3.
- Find (a^b)%m where 'b' is very large
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Number of factors of very large number N modulo M where M is any prime number
- Print all K digit repeating numbers in a very large number
- How to store a very large number of more than 100 digits in C++
- Multiply large integers under large modulo
- Sum of all natural numbers from L to R ( for large values of L and R )
- All possible values of floor(N/K) for all values of K
- Check if a large number is divisible by 2, 3 and 5 or not
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Factorial of a large number
- Find Last Digit of a^b for Large Numbers
- Remainder with 7 for large numbers
- Sum of two large numbers
- Check if a large number is divisible by 11 or not

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.