Given two integer numbers X and Y. find the larger of X^Y and Y^X, or determine if they are equal.
Input : 2 3 Output : 3^2 We know 3^2 = 9 and 2^3 = 8. Input : 2 4 Output : Equal
A simple solution is calculate x^y by looping for y times, but if the values of x and y is too large it will cause overflow.
To solve the overflow problem, we can simplify the equation by taking log.
log(x^y) = y* log(x)
Now, this equation will not cause overflow and we can compare two values directly.
# Python3 program to print greater
# of x^y and y^x
def printGreater(x, y):
X = y * math.log(x);
Y = x * math.log(y);
if (abs(X – Y) < 1e-9): print("Equal"); elif (X > Y):
print(x, “^”, y);
print(y, “^”, x);
# Driver Code
x = 5;
y = 8;
# This code is contributed by mits
- Smallest triangular number larger than p
- Check if a larger number divisible by 36
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find 2^(2^A) % B
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find the value of f(n) / f(r) * f(n-r)
- Find sum of the series 1-2+3-4+5-6+7.......
- Find the sum of series 3, 7, 13, 21, 31....
- Find any pair with given GCD and LCM
- Find the sum of the series 2, 5, 13, 35, 97...
- Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ ... + x^n(x^n+y^n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.