# Comparing X^Y and Y^X for very large values of X and Y

Given two integer X and Y, the task is compare XY and YX for large values of X and Y.

Examples:

Input: X = 2, Y = 3
Output: 2^3 < 3^2
23 < 32

Input: X = 4, Y = 5
Output: 4^5 > 5^4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: A basic approach is to find the values XY and YX and compare them which can overflow as the values of X and Y can be large

Better approach: Taking log of both the equations, log(XY) = Y * log(X) and log(YX) = X * log(Y). Now, these values can be compared easily without overflows.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `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; ` `} `

## 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 `

## 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 `

## 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.  `

Output:

```4^5 > 5^4
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : AnkitRai01, jit_t, 29AjayKumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.