Given four integers A, B, C and D. The task is to find which is greater AB or CD.
Input: A = 2, B = 5, C = 4, D = 2
25 = 32
42 = 16
Input: A = 8, B = 29, C = 60, D = 59
Naive approach: Calculate the values of AB and CD then compare them. This approach will fail when the values are greater say 562145321457.
Efficient approach: Using log, we can write the terms as log(AB) and log(CD) which can also be written as B * log(A) and D * log(C). These values are easier to calculate and compare than the original values.
Below is the implementation of the above approach:
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.
- Exponential Squaring (Fast Modulo Multiplication)
- Exponential notation of a decimal number
- Count of exponential paths in a Binary Tree
- Print all Exponential Levels of a Binary Tree
- Find larger of x^y and y^x
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Check if a larger number divisible by 36
- Smallest triangular number larger than p
- 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)
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Repeated subtraction among two numbers
- Find position of the given number among the numbers made of 4 and 7
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Program to find the smallest element among three elements
- Find maximum product of digits among numbers less than or equal to N
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Sum of bit differences among all pairs
- Position of n among the numbers made of 2, 3, 5 & 7
- Count ways to distribute m items among n people
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.