Given two integer X and Y, the task is compare XY and YX for large values of X and Y.
Input: X = 2, Y = 3
Output: 2^3 < 3^2
23 < 32
Input: X = 4, Y = 5
Output: 4^5 > 5^4
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:
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.
- 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 email@example.com. 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.